[英]How to add Navigation bar to view controller with tab bar item
I have an app that uses bottom tabs aswell as a side menu, to have the button that initiates the side menu i use the typical three line menu button, to put that there I have a Navigation Bar. 我有一个使用底部选项卡以及侧面菜单的应用程序,具有启动侧面菜单的按钮,我使用了典型的三行菜单按钮,并在其中放置了导航栏。 With the bar in place there is no way I can get the bar to be on top of the screen.
将栏放置在适当的位置,我无法将栏放在屏幕顶部。 I built it with interface builder, and heres a screenshot.
我使用界面生成器构建了它,下面是截图。 The question is how do i have the navigation bar alone without the other grey bar above it?
问题是我如何单独拥有导航栏,而上方没有其他灰色栏?
The issue you're encountering is due to the fact that you're manually creating a navigation bar for your view controller, instead of using the bar that you get for free by embedding the view controller in a tab bar controller, hence the reason you see two bars. 您遇到的问题是由于您正在为视图控制器手动创建导航栏,而不是通过将视图控制器嵌入到标签栏控制器中来免费使用获得的导航栏,因此您看到两个酒吧。 The other answer suggesting hiding the auto-generated navigation bar is not the correct solution.
另一个建议隐藏自动生成的导航栏的答案不是正确的解决方案。 Instead, you should place your menu button and view title in the auto-generated bar instead of manually creating your own (you almost never want to do that, in-fact).
相反,您应该将菜单按钮和视图标题放在自动生成的栏中,而不要手动创建自己的菜单(实际上,您几乎从不希望这样做)。
So what you should do instead is set the title
property of your view controller to be "News", and the leftBarButtonItem
property of the view controller to be your hamburger menu button (an instance of UIBarButtonItem
initialized with an image for the icon). 因此,您应该做的是将视图控制器的
title
属性设置为“ News”,并将视图控制器的leftBarButtonItem
属性设置为您的汉堡菜单按钮( UIBarButtonItem
的实例初始化为该图标的图像)。
For example (inside your view controller's viewDidLoad
method or wherever appropriate): 例如(在您的视图控制器的
viewDidLoad
方法内部或适当的地方):
self.title = @"News";
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"menuIcon"] style:UIBarButtonItemStylePlain target:self action:@selector(showSideMenu)];
If you want to remove the topmost navigation bar you need use self.navigationController.navigationBarHidden = YES;
如果要删除最上面的导航栏,则需要使用
self.navigationController.navigationBarHidden = YES;
for view controllers that used for tabs in UITabBarController
: 对于用于
UITabBarController
选项卡的视图控制器:
// StoriesViewController.m
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.navigationController.navigationBarHidden = YES;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.