有個UINavigationController
,需要顯示導航條按鈕,代碼如下:
UINavigationController *nav=[[UINavigationController alloc] init];
[self.view addSubview:nav.view];
UIImage *info_iphone=[UIImage imageNamed:@"button.png"];
UIButton *infobtn=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 48, 30)];
[infobtn setBackgroundImage:info_iphone forState:UIControlStateNormal];
[infobtn addTarget:self action:@selector(show_info:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:infobtn];
如果上述的代碼沒有實現你要的效果。首先可能的原因就是當前控制器viewcontroller 不在導航控制器棧中。因為只有處於導航控制器棧中的控制器viewcontroller,你在設置了它的UIBarButtonItem後才會被顯示出來。