Tôi đã thử hầu hết các giải pháp được đề xuất.Cuối cùng không ai trong số họ làm việc cho tôi.
hideTabBarWhenPushed ẩn thanh tab không chỉ cho bộ điều khiển chế độ xem được đẩy tiếp theo mà còn cho tất cả bộ điều khiển chế độ xem được đẩy bên trong. Đối với những người tôi đã muốn điều khiển thanh tab để tái xuất hiện.
Giải pháp của Orafaelreis (xem bên trên) dường như có nhiều nhất. Nhưng nỗ lực của ông chỉ làm việc cho các định hướng chân dung nghiêm ngặt, thậm chí không bị lộn ngược. Vì vậy, tôi đã phải sửa chữa nó. Đây là những gì tôi đã nhận được cuối cùng:
#define kTabBarHeight 49 // This may be different on retina screens. Frankly, I have not yet tried.
- (void) hideTabBar:(BOOL)hide {
// fetch the app delegate
AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
// get the device coordinates
CGRect bounds = [UIScreen mainScreen].bounds;
float width;
float height;
// Apparently the tab bar controller's view works with device coordinates
// and not with normal view/sub view coordinates
// Therefore the following statement works for all orientations.
width = bounds.size.width;
height = bounds.size.height;
if (hide) {
// The tab bar should be hidden too.
// Otherwise it may flickr up a moment upon rotation or
// upon return from detail view controllers.
[self.tabBarController.tabBar setHidden:YES];
// Hiding alone is not sufficient. Hiding alone would leave us with an unusable black
// bar on the bottom of the size of the tab bar.
// We need to enlarge the tab bar controller's view by the height of the tab bar.
// Doing so the tab bar, although hidden, appears just beneath the screen.
// As the tab bar controller's view works in device coordinations, we need to enlarge
// it by the tab bar height in the appropriate direction (height in portrait and width in landscape)
// and in reverse/upside down orientation we need to shift the area's origin beyond zero.
switch (delegate.tabBarController.interfaceOrientation) {
case UIInterfaceOrientationPortrait:
// Easy going. Just add the space on the bottom.
[self.tabBarController.view setFrame:CGRectMake(0,0,width,height+kTabBarHeight)];
break;
case UIInterfaceOrientationPortraitUpsideDown:
// The bottom is now up! Add the appropriate space and shift the rect's origin to y = -49
[self.tabBarController.view setFrame:CGRectMake(0,-kTabBarHeight,width,height+kTabBarHeight)];
break;
case UIInterfaceOrientationLandscapeLeft:
// Same as Portrait but add the space to the with but the height
[self.tabBarController.view setFrame:CGRectMake(0,0,width+kTabBarHeight,height)];
break;
case UIInterfaceOrientationLandscapeRight:
// Similar to Upside Down: Add the space and shift the rect. Just use x and with this time
[self.tabBarController.view setFrame:CGRectMake(0-kTabBarHeight,0,width+kTabBarHeight,height)];
break;
default:
break;
}
} else {
// reset everything to its original state.
[self.tabBarController.view setFrame:CGRectMake(0,0,width,height)];
[self.tabBarController.tabBar setHidden:NO];
}
return;
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
// It is important to call this method at all and to call it here and not in willRotateToInterfaceOrientation
// Otherwise the tab bar will re-appear.
[self hideTabBar:YES];
// You may want to re-arrange any other views according to the new orientation
// You could, of course, utilize willRotateToInterfaceOrientation instead for your subViews.
}
- (void)viewWillAppear: (BOOL)animated {
// In my app I want to hide the status bar and navigation bar too.
// You may not want to do that. If so then skip the next two lines.
self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
[self hideTabBar: YES];
// You may want to re-arrange your subviews here.
// Orientation may have changed while detail view controllers were visible.
// This method is called upon return from pushed and pulled view controllers.
return;
}
- (void)viewWillDisappear: (BOOL)animated {
// This method is called while this view controller is pulled
// or when a sub view controller is pushed and becomes visible
// Therefore the original settings for the tab bar, navigation bar and status bar need to be re-instated
[self hideTabBar:NO];
// If you did not change the appearance of the navigation and status bar in viewWillAppear,
// then you can skip the next two statements too.
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationSlide];
return;
}
Nhận xét nội dòng nên giải thích lý do cho từng câu. Mặc dù, có thể có cách mã hóa thông minh hơn.
Có một tác dụng phụ cùng với việc ẩn thanh trạng thái và thanh điều hướng, mà tôi không muốn ẩn khỏi các bạn. 1. Khi trở về từ bộ điều khiển điều hướng này đến bộ điều khiển chuyển hướng gọi thì thanh trạng thái và thanh điều hướng trên bộ điều khiển gọi chồng chéo cho đến khi thiết bị được xoay một lần hoặc cho đến khi tab liên quan được chọn lại sau khi một tab khác xuất hiện. 2. Khi bộ điều khiển chế độ xem cuộc gọi là chế độ xem bảng và khi thiết bị ở chế độ nằm ngang khi quay lại bảng, thì bảng được hiển thị theo hướng thích hợp cho phong cảnh nhưng nó được bố trí như thể nó là chân dung. Góc trên bên trái là tốt nhưng một số ô bảng cộng với thanh tab được ẩn bên dưới màn hình. Ở phía bên phải có một số không gian trống. Điều này cũng được cố định bằng cách xoay thiết bị lần nữa.
Tôi sẽ cập nhật cho bạn khi tôi tìm thấy giải pháp cho các lỗi nhỏ nhưng khó chịu này.
Điều này chắc chắn là tốt hơn. Cảm ơn bạn! – givp
Một điều cần nhớ: bạn phải đặt mã trên TRƯỚC KHI đẩy nó vào bộ điều khiển. Đó là ngay sau dòng khởi tạo. Nó sẽ KHÔNG hoạt động nếu bạn đặt nó trong viewDidLoad hoặc nơi tương tự ... – Lukasz
Rất đẹp, đã giải quyết giờ làm việc của tôi! – Rahul