我正在制作一个 iPhone 应用程序,并希望在我的导航栏的 titleview 位置显示一个 Logo 。我创建了 UINavigationController 的子类,并在 viewDidLoad 中添加了以下行:
self.navigationBar.topItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"titlelogo.png"]];
(这似乎只是一行的浪费,但我在多个地方使用它并且出于各种原因我不想使用 [UINavigationBar appearance])
这适用于导航 Controller 的直接子级 - 我看到了 Logo 。但是,当我将 segue 推到另一个 View 时, Logo 不会保留,即使它们(据我所知)是“相同”导航 Controller 的一部分。
(供引用)-> MyNavigationController -> SomeViewController -> SomeOtherViewController
我还尝试使用以下方法进入 SomeOtherViewController 的 viewDidLoad:
self.navigationController.navigationBar.topItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"titlelogo.png"]];
这仍然没有显示 Logo 。
经过一番探索,我发现如果 1) 我在导航 Controller 的 viewDidLoad 中没有那一行,并且 2) 我在 SomeOtherViewController 的 viewDidLoad 中有第二行,那么:
开始:查看 SomeViewController。没有标志。 单击以推送 segue: Logo 现在出现在 SomeViewController 的栏上,然后消失。 我现在正在查看 SomeOtherViewController: 没有 Logo 。 点击返回:SomeViewController的导航栏仍然有logo。
显然我误解了导航 Controller 的工作原理。谁能告诉我如何解决这个问题?
最佳答案
如果我没理解错的话,你也可以用另一种方法来做,只用一张图片替换整个 NavigationBar 吗?在那里你可以放置你的图标。
因为更改整个 NavigationBar 非常容易。
if ([self.navigationBar respondsToSelector:@selector( setBackgroundImage:forBarMetrics:)]) {
[self.navigationBar setBackgroundImage:[UIImage imageNamed:@"navigationbar.png"] forBarMetrics:UIBarMetricsDefault];
}
您需要将此代码放入NavigationController 的viewDidLoad
方法中。然后它也应该在你推送到 NavigationController 堆栈上的 ViewControllers 中工作。
关于iphone - 导航栏上的 titleview 在 segues 后的 View 中不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10306671/