简体   繁体   English

如何使用标签栏项添加导航栏以查看控制器

[英]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.

相关问题 如何在没有视图控制器的情况下将标签栏项添加到UITabBarController? - How to add a tab bar item to UITabBarController without a view controller? 将标签栏添加到导航控制器 - Add Tab Bar to Navigation Controller 如何在导航栏、标签栏、视图控制器之间传递数据 - How to pass data among Navigation bar, Tab bar, View Controller Swift-如何将Tab Bar和导航栏添加到单个视图控制器? - Swift-How do I add Tab Bar AND Navigation Bar to a single view controller? 标签栏控制器中每个视图的不同导航项 - Different navigation item for each view in tab bar controller 条形图按钮项仅在一个标签栏控制器导航栏中 - Bar Button Item in only one of the tab bar controller navigation bar 如何将视图控制器推送到iOS中标签栏中的导航控制器 - how to push a view controller to a navigation controller in a tab bar in iOS 在视图控制器上添加导航栏 - Add navigation bar on a view controller 从导航栏视图控制器导航到选项卡栏视图控制器 - Navigate from a navigation bar view controller to a tab bar view controller 使用 Tab Bar Controller Swift 在导航栏中添加新的栏按钮 - Add new bar button in Navigation Bar with a Tab Bar Controller Swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM