UINavigationBar右按钮未显示 [英] UINavigationBar right button not showing up

查看:70
本文介绍了UINavigationBar右按钮未显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过通知使用以下函数在UINavigationBar上加载一个右键,即使我可以找出该按钮并确认已分配该按钮,也不会显示...任何想法?

I am using the following function via a notification to load a right button on my UINavigationBar, and even though I can trace out the button and verify it is allocated, it does not show up...any ideas?

编辑4/6/2011,2:42 PM

EDIT 4/6/2011, 2:42PM

所以,有趣的是...宽度总是报告为0.0 ...

So, something interesting...the width always reports as 0.0...

- (void)showRightBarButton:(id)sender
{
    NSLog(@"Showing button");
    UIBarButtonItem *button = [[UIBarButtonItem alloc]
                               initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
                               target:self
                               action:@selector(showPI:)];
    [button setTitle:@"This Button"];
    self.navigationItem.rightBarButtonItem = button;
    //[[self.navigationItem rightBarButtonItem] setWidth:50];
    NSLog(@"Button width is %f.", self.navigationItem.rightBarButtonItem.width);
    [button release];
}


推荐答案

[self.view setNeedsDisplay];

您是对的。不需要那条线。至于其余的代码,我看不出这是怎么回事。到目前为止,我唯一想到的是自己不是当前显示的视图控制器,或者您缺少导航控制器。也许您是自己创建了 UINavigationBar 而不是使用导航控制器?

You're right. That line isn't needed. As far as the rest of the code goes I don't see what's wrong with it. The only thing I've come up with so far is that self isn't the currently displayed view controller or that you're missing a navigation controller. Perhaps you've created your UINavigationBar yourself instead of using a navigation controller?

无论如何,为了简化调试,我会建议以下内容:

Anyway, for easier debugging I would suggest the following:

- (void)showRightBarButton:(id)sender
{
    NSLog(@"Showing button");
    UIBarButtonItem *button = [[UIBarButtonItem alloc]
                             initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
                                                  target:self
                                                  action:@selector(showPI:)];
    self.navigationItem.rightBarButtonItem = button;
    [button release];
}

编辑:宽度并不有趣。除非您自己指定,否则始终为0.0。

The width isn't interesting. It's always 0.0 unless you specify it yourself.

问题是您将按钮添加到错误的位置。您不应该将按钮添加到导航控制器的导航项中,而是添加到导航控制器当前显示的控制器的导航项中。

The problem is that you're adding the button in the wrong place. You're not supposed to add the button to the navigation item of the navigation controller, but to the navigation item of the controller that is currently displayed by the navigation controller.

这篇关于UINavigationBar右按钮未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆