titleView 仅适用于一个视图控制器(self.navigationController 为 nil)
我有一些导航控制器设置在选项卡控制器下的 NIB 中。我试图在每个导航控制器的顶视图控制器中设置相同的徽标。
在出现的第一个视图控制器中,我在 viewDidLoad
中有以下代码:
self.navigationItem.titleView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"green-noback-logo-only.png"]] autorelease];
这有效(好吧,几乎,我必须调整图像大小)并用我的徽标替换 NIB 中设置的文本图像。
但是,这个完全相同的代码在其他两个视图控制器中都不起作用。相反,我在 NIB 中为标题设置的任何文本都会显示。我尝试将该代码放入 initWithCoder、viewDidLoad、viewDidAppear
和 viewWillAppear
中,但它什么也没做。我明确地将 leftBarButtonItem
设置为 nil,尽管我猜测它一开始就是 nil。我还已经检查过 self.navigationItem 在我尝试设置 titleView 的任何地方都不为零。
知道其他两个控制器有什么特别之处,会阻止它们设置 titleView 吗?否则,有人有更简单的方法来设置 titleView 吗?
I have a few navigation controllers that are set up in a NIB under a tab controller. I'm trying to set up the same logo in the top view controller of each navigationcontroller.
In the first view controller that appears, I have this code in viewDidLoad
:
self.navigationItem.titleView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"green-noback-logo-only.png"]] autorelease];
This works (well, almost, I'll have to resize the image) and replaces the text set up in the NIB with my logo image.
However, this exact same code doesn't work in either of the other two view controllers. Instead, any text I've set up for the title in the NIB shows. I've tried putting that code in initWithCoder, viewDidLoad, viewDidAppear
and viewWillAppear
and it does nothing. I'm explicitly setting leftBarButtonItem
to nil, although I'm guessing it was nil to begin with. I have also already checked that self.navigationItem is not nil in any of the places where I'm trying to set the titleView.
Any idea what would be special about the other two controllers that would prevent them from having a titleView set? Otherwise, does someone have a more foolproof way to set titleView?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试将图像视图设置为
self.navigationController.navigationItem
而不是self.navigationItem
。try setting the image view to
self.navigationController.navigationItem
instead ofself.navigationItem
.tl;dr:我搞砸了
initWithCoder:
。对于初学者来说是一个很好的墙工。根据 Paul N 的回答,我发现在两个损坏的视图控制器中
self.navigationController == nil
。我又花了几个小时的时间才弄清楚剩下的事情。所有三个顶级视图控制器都是 UITableViewController 的子类。然而,其中只有两个使用分组样式。我重写了 initWithCoder: 来在两个非工作表视图控制器中使用 initWithStyle: 。这丢弃了与存储在 NIB 中的导航控制器的连接。我最初这样做是因为我无法弄清楚如何在 NIB 内设置分组样式(由 这里是另一个答案)。
我想,以如此糟糕的方式进行子类化是正确的。
不管怎样,解决方案是修复
initWithCoder:
实现,像往常一样调用[super initWithCoder:coder]
并在 NIB 中设置表格视图样式。我通过在该视图控制器下拖动表视图、设置数据源、设置委托并将其设置为分组样式来完成此操作。 (这就是默认情况下在 NIB 中设置表视图控制器的方式。)tl;dr: I screwed up
initWithCoder:
.A good wallbanger for a beginner. Following Paul N's answer, I discovered that
self.navigationController == nil
in the two broken view controllers. It took me another few hours of head-desking to figure out the rest.All three top level view controllers were subclasses of UITableViewController. However, only two of them were using grouped style. I was overriding
initWithCoder:
to useinitWithStyle:
inside the two non-working table view controllers. This threw away the connection to the navigation controller stored in the NIB. I originally did this because I couldn't figure out how to set grouped style on those inside the NIB (suggested by another answer here).Serves me right for subclassing in such a rotten fashion, I guess.
Anyway, the solution was to fix the
initWithCoder:
implementation to call[super initWithCoder:coder]
as usual and set up the table view style in the NIB. I did this by dragging a table view under that view controller, setting the datasource, setting the delegate, and setting it to grouped style. (This is how table view controllers are set up in the NIB by default.)