简体   繁体   English

导航栏上的titleview在segues之后的视图中不可见

[英]titleview on navigation bar not visible in views after segues

I am making an iPhone application, and would like to display a logo in the titleview position of my navigation bar. 我正在制作一个iPhone应用程序,并希望在导航栏的titleview位置显示一个徽标。 I've made a subclass of UINavigationController and added the following line in viewDidLoad: 我已经创建了UINavigationController的子类,并在viewDidLoad中添加了以下行:

self.navigationBar.topItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"titlelogo.png"]];

(this may seem like a waste for just one line, but i'm using it in multiple places and I don't want to use [UINavigationBar appearance] for various reasons) (这似乎只是浪费一条线,但我在多个地方使用它,我不想出于各种原因使用[UINavigationBar外观])

This works fine for the immediate child of the navigation controller - I see the logo. 这适用于导航控制器的直接孩子 - 我看到了徽标。 However, when I push segue to another view, the logo does not carry over, even though they are (to the best of my knowledge) part of the "same" navigation controller. 但是,当我将segue推到另一个视图时,徽标不会延续,即使它们(据我所知)是“相同”导航控制器的一部分。

(for reference) -> MyNavigationController -> SomeViewController -> SomeOtherViewController (供参考) - > MyNavigationController - > SomeViewController - > SomeOtherViewController

I have also tried going into the viewDidLoad of SomeOtherViewController with a: 我也尝试使用以下命令进入SomeOtherViewController的viewDidLoad:

 self.navigationController.navigationBar.topItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"titlelogo.png"]];

This still does not show the logo. 这仍然没有显示徽标。

After some poking around, I have discovered that if 1) I do not have that line in the navigationcontroller's viewDidLoad and 2) I do have the second line in the SomeOtherViewController's viewDidLoad, then: 经过一番探讨,我发现如果1)我在导航控制器的viewDidLoad中没有该行,2)我在SomeOtherViewController的viewDidLoad中有第二行,那么:

Start: Viewing SomeViewController. 开始:查看SomeViewController。 No logo. 没有标志。 Click to push segue: The logo now appears on SomeViewController's bar, before disappearing. 点击推送segue:徽标现在出现在SomeViewController的栏上,然后消失。 I am now viewing SomeOtherViewController: which has no logo. 我现在正在查看SomeOtherViewController:它没有徽标。 Click back: SomeViewController's navigation bar still has the logo. 单击返回:SomeViewController的导航栏仍然具有徽标。

Obviously I am misunderstanding something about how a navigationcontroller works. 显然我误解了导航控制器的工作原理。 Can anyone tell me how to fix this? 谁能告诉我如何解决这个问题?

If I understand it right you could also do it with another approach and just replace the whole NavigationBar with an image? 如果我理解正确你也可以用另一种方法来做,只需用图像替换整个NavigationBar There you could place your icon. 在那里你可以放置你的图标。

Because changing the whole NavigationBar is quite easy. 因为改变整个NavigationBar非常容易。

if ([self.navigationBar respondsToSelector:@selector( setBackgroundImage:forBarMetrics:)]) {
  [self.navigationBar setBackgroundImage:[UIImage imageNamed:@"navigationbar.png"] forBarMetrics:UIBarMetricsDefault];
}

This code you need to put in the viewDidLoad method of the NavigationController . 您需要将此代码放在NavigationControllerviewDidLoad方法中。 Then it should also be working in ViewControllers you push on the stack of the NavigationController. 然后它也应该在ViewControllers中工作,你可以推送NavigationController的堆栈。

Try to use 尝试使用

self.navigationItem.titleView = yourCustomView;

istead of 不是

self.navigationController.navigationBar.topItem.titleView = yourCustomView;

And do not touch self.navigationController.navigationBar.topItem.titleView at all. 并且不要触摸self.navigationController.navigationBar.topItem.titleView At first I tried to set both of them to yourCustomView , and my custom view appeared for a split second and then was gone I have no idea where :) 起初我尝试将它们都设置为yourCustomView ,我的自定义视图出现了一瞬间然后消失了我不知道在哪里:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 故事板:改变segues后推断导航栏 - Storyboard: inferring navigation bar after changing segues 导航栏titleView对齐 - Navigation bar titleView alignment 编辑导航栏titleView时未显示inputAccessoryView - inputAccessoryView not shown when editing navigation bar titleView 如何为所有导航控制器和视图控制器设置自定义导航栏(标题+ titleView)? - How do I set custom navigation bar(title + titleView) for all navigation controllers and view controller? 即使实现了PushView控制器后,如何使导航栏和选项卡栏始终在每个屏幕上可见 - How to make Navigation Bar and Tab bar always visible on every screen even after implementing PushView controller iOS:当手机旋转时,导航栏的titleView无法正确调整大小 - iOS: navigation bar's titleView doesn't resize correctly when phone rotates 手动segue时导航栏不可见 - Navigation bar not visible when manual segue 取消在蓝色导航栏中的MFMessageComposeViewController上不可见 - Cancel not visible on MFMessageComposeViewController in blue color navigation bar 使用segues自动旋转视图 - Using segues to automatically rotate views 设置“更多导航控制器的”标题查看文本 - Setting “More Navigation Controller's” titleView text
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM