xcode 导航标题未显示在选项卡视图控制器的视图中,但“返回”导航有效

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21615574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 04:37:55  来源:igfitidea点击:

Navigation title not showing on view with tab view controller, but "back" navigation works

iosxcodeuinavigationcontrolleruinavigationitemuitabview

提问by edparry

I'm relatively new to iOS Objective-C development, and I've come across a problem that I can't find a solution to.

我对 iOS Objective-C 开发比较陌生,遇到了一个找不到解决方案的问题。

I have a Table View Controller, with two prototype cells on it, which populate fine. This Table View Controlleris one of three Tab Views, and the Viewthat sends to the Tab Viewshas a Navigation Controller. This means that the views within the Tab Viewsalso have a Navigationbar. The bar works fine, in terms of the "back" button working as expected, and the bar being in position. However, (at least on the List View) the Navigation Barisn't fully recognised - it's title doesn't appear, and the table cells start directly below the status bar, rather than below the navigation bar.

我有一个Table View Controller, 上面有两个原型单元格,可以很好地填充。这Table View Controller是三选一Tab Views,并且View发送到Tab ViewsNavigation Controller。这意味着内部的意见Tab Views也有一个Navigation酒吧。栏工作正常,就“后退”按钮按预期工作而言,并且栏就位。但是,(至少在列表视图中)Navigation Bar没有被完全识别 - 它的标题没有出现,表格单元格直接在状态栏下方开始,而不是在导航栏下方。

Here's a couple of screenshots showing the problem: what appears in Xcode (what I expect to happen)what appears in Xcode (what I expect to happen) what actually appears on my testing device (iPhone 4, iOS 7.0.4)And then on the device, this is what actually appears - the Back button in place and working fine, but no title field, and the table cells start too high.

这是显示问题的几个屏幕截图: what appears in Xcode (what I expect to happen)Xcode 中出现的内容(我期望发生的情况) what actually appears on my testing device (iPhone 4, iOS 7.0.4)然后在设备上,这就是实际出现的内容 - 后退按钮就位并且工作正常,但没有标题字段和表格单元格起点太高。

I've tried adding Navigation Bar's and Navigation Items, and while adding a Navigation Itemallows me to put a title on in Xcode, it still doesn't appear on the device in testing. I also tried to add another Navigation Controllerjust before this view, but that didn't resolve the issue either, and it caused navigation problems further down in the heirachy.

我尝试添加Navigation Bar's 和Navigation Items,虽然添加 aNavigation Item允许我在 Xcode 中放置标题,但它仍然没有出现在测试中的设备上。我还尝试Navigation Controller在此视图之前添加另一个,但这也没有解决问题,并且导致了更远的层次结构中的导航问题。

Hope I've been clear enough, please say if I need to post more information - I'm relatively new to Xcode and so not sure what exactly is applicable and what isn't. Thanks!

希望我已经足够清楚,如果我需要发布更多信息,请说 - 我对 Xcode 比较陌生,所以不确定什么是适用的,什么不是。谢谢!

回答by Xu Yin

please try this code, it might fix your table position

请试试这个代码,它可能会修复你的桌子位置

// Since in iOS7 the nav bar is translucent by default, so the table view starts at (0,0)
// you can either disable the translucent, which i don't recommend unless you really want to
// or just add 64 pixel on the top of your table view
[self.YOURTABLEVIEW setContentInset:UIEdgeInsetsMake(64, 0, 0, 0)];

and for the title, please try this

和标题,请试试这个

self.tabBarController.navigationItem.title =@"YOUT TITLE NAME";

Hope that helps..

希望有帮助..

回答by Nidhin

Assuming your hierarchy as

假设您的层次结构为

NavigationController -> ViewController  -> TabBarController -> ViewController1
                                                            -> ViewController2
                                                            -> ViewController3

If you want to hide navigation item in viewcontroller1, Add the following line

如果要隐藏viewcontroller1中的导航项,添加以下行

 self.navigationController.navigationBarHidden = YES;

If you want to show title in viewcontroller2, Add the following line in

如果要在 viewcontroller2 中显示标题,请添加以下行

self.navigationController.navigationBarHidden = NO; //add this if you hide navItem viewcontroller1
[self.parentViewController.navigationItem setTitle:@"Title"];

If you want to hide backbutton and show title in viewcontroller3, Add the following line

如果要隐藏后退按钮并在 viewcontroller3 中显示标题,请添加以下行

 self.navigationController.navigationBarHidden = NO;
[self.parentViewController.navigationItem setTitle:@"Contacts"];

self.parentViewController.navigationItem.hidesBackButton=YES; 

Add this lines to viewdidAppear method instead of ViewdidLoad ,if you have problems inshowing when switching tabs.

将此行添加到 viewdidAppear 方法而不是 ViewdidLoad ,如果您在切换标签时遇到问题。

回答by vidalbenjoe

Try to click the Navigation Bar from your storyboard or nib.

尝试从故事板或笔尖单击导航栏。

enter image description here

enter image description here

Then add your title to the property.

然后将您的标题添加到属性中。

enter image description here

enter image description here

回答by user2593040

I had the same problem, but what I did to create this problem was that my buttons action was connecting to the actual table itself and not the table Controller. I removed the modal action and created a new action to the table controller and it fixed the problem.

我遇到了同样的问题,但我为创建这个问题所做的是我的按钮操作连接到实际表本身而不是表控制器。我删除了模态动作并为表控制器创建了一个新动作,它解决了问题。