右UIBarButton未显示

时间:2013-07-02 15:42:58

标签: ios

这段代码很简单:

-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// Inizializzazione barra di navigazione
[[self navigationController] setNavigationBarHidden:NO];

UINavigationItem* a = [self navigationItem];
[a setTitle:@"SOME TITLE"];
UIImage *background = [UIImage imageNamed:@"header.png"];
CGSize newSize;
newSize.height=100;
[self.navigationController.navigationBar setBackgroundImage:background 
                                         forBarMetrics:UIBarMetricsDefault];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] 
                 initWithTitle:@"Homefgh"                                                                                  
                 style:UIBarButtonItemStyleBordered 
                 target:self action:@selector(home)];

self.navigationController.navigationItem.rightBarButtonItem = rightButton;
}

但未显示按钮(右侧)。 我还尝试将代码放在viewDidLoad中。 viewWillAppear位于主UINavigationController顶部的UIView内。 奇怪的是背景图像正确显示。

4 个答案:

答案 0 :(得分:3)

你应该可以做一些简单的事情来让它出现:

UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] 
                 initWithTitle:@"Homefgh"                                                                                  
                 style:UIBarButtonItemStyleBordered 
                 target:self action:@selector(home)];

self.navigationItem.rightBarButtonItem = rightButton;

答案 1 :(得分:0)

self.navigationController.navigationItem.rightBarButtonItem = rightButton;更改为self.navigationItem.rightBarButtonItem = rightButton;

答案 2 :(得分:0)

- (void)viewDidLoad {
  [super viewDidLoad];

  UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:@"Show" style:UIBarButtonItemStylePlain target:self action:@selector(refreshPropertyList:)];          
  self.navigationItem.rightBarButtonItem = anotherButton;
  [anotherButton release];
}

请记住将其放在viewDidLoad中而不是viewWillAppear。

答案 3 :(得分:0)

在您的代码中添加此内容

self.navigationItem.rightBarButtonItem = rightButton;

而不是这段代码

self.navigationController.navigationItem.rightBarButtonItem = rightButton;

它会显示出来。