【发布时间】:2023-04-02 04:33:01
【问题描述】:
我在 UITabBarController(选项卡 1)中有一个 UINavigationController。当我进入第二个视图(仍在选项卡 1 中)时,如何使选项卡栏消失?我可以使用返回按钮向后导航,标签栏将重新出现。
【问题讨论】:
标签: iphone objective-c xcode ios
我在 UITabBarController(选项卡 1)中有一个 UINavigationController。当我进入第二个视图(仍在选项卡 1 中)时,如何使选项卡栏消失?我可以使用返回按钮向后导航,标签栏将重新出现。
【问题讨论】:
标签: iphone objective-c xcode ios
self.hidesBottomBarWhenPushed=YES; 将此行放在您导航的位置(在推送操作之前)。
和 self.hidesBottomBarWhenPushed=NO; 在 viewWillDisappear 从您推送其他视图的同一页面中消失。
确实有效。
【讨论】:
.hidesBottomBarWhenPushed 设置为 self 不会做任何事情,您需要在被推送的 VC 上进行设置。
在被推送的viewController中,放:
self.hidesBottomBarWhenPushed = YES;
在-viewDidLoad 方法中。它属于“子”VC,而不是进行推送的 VC。您无需在其他任何地方设置它。
【讨论】:
我喜欢使用视图控制器的 init 方法来隐藏底部栏,等等。更好地封装行为。
(注意:以下是 ARC 友好的代码,因此没有 autorelease 调用或 retain/release 对。)
#pragma mark - UIViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
// We must handle this as it's the designated initializer for UIViewController.
// Pay no attention to the params. We're going to override them anyway.
return [self init];
}
#pragma mark - NSObject
- (id)init {
// Why hello there, superclass designated initializer! How are you?
if ((self = [super initWithNibName:@"YourNibNameHere" bundle:nil])) {
// This is a perfect oppy to set up a number of things, such as ...
// ... the title (since you're in a nav controller).
self.navigationItem.title = @"Your Nav Title";
// ... your bottom bar hiding (takes effect once pushed onto your nav controller).
self.hidesBottomBarWhenPushed = YES;
// ... and your tab bar item (since you're in a tab bar controller).
[self setTabBarItem:[[UITabBarItem alloc] initWithTitle:@"Item Title" image:[UIImage imageNamed:@"itemIcon.png"] tag:itemTag]];
}
return self;
}
现在你要做的就是alloc/init你的视图控制器并调用-pushViewController:animated:。没有麻烦,没有大惊小怪。
当弹出 VC 时,您的底栏将返回。 (承诺。)
这项技术的功劳归功于 Big Nerd Ranch 的 Joe Conway。 (我就是从他那里学到了这个绝妙的模式。)
至于使用点符号还是不使用,那是完全不同的讨论。 YMMV。 ;)
【讨论】: