0

就像这段代码一样简单:

-(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

4 回答 4

3

你应该能够做一些像这样简单的事情来让它显示出来:

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

self.navigationItem.rightBarButtonItem = rightButton;
于 2013-07-02T15:46:03.940 回答
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 中。

于 2013-07-02T15:50:56.910 回答
0

在你的代码中添加这个

self.navigationItem.rightBarButtonItem = rightButton;

而不是这段代码

self.navigationController.navigationItem.rightBarButtonItem = rightButton;

并且会显示出来。

于 2013-07-02T15:52:26.687 回答
0

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

于 2013-07-02T15:49:39.030 回答