rightbarbuttonitem不显示

我有下面的代码,把rightbarbuttonitem

UIButton* rightbutton = [UIButton buttonWithType:UIButtonTypeCustom]; [rightbutton setBackgroundImage:[UIImage imageNamed:@"share-icon.png"] forState:UIControlStateNormal]; [rightbutton addTarget:self action:@selector(share:) forControlEvents:UIControlEventTouchUpInside]; self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:rightbutton] autorelease]; 

但它没有展示任何barbuttonitem。 相反,如果我使用下面的代码然后barbutton项目出现,但问题是我不能设置触摸事件与此代码barbuttonitem。

 UIImageView *iconView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"dashboard-icon.png"]]; UIBarButtonItem *icon = [[UIBarButtonItem alloc] initWithCustomView:iconView]; self.navigationItem.leftBarButtonItem=icon; [icon release]; [iconView release]; 

你有没有试图为rightbutton设置适当的框架? 例如rightbutton.frame = (CGRect){CGPointZero, image.size};

另请注意:

在iOS 4及更高版本中,文件的名称不需要指定文件扩展名。 在iOS 4之前,您必须指定文件扩展名。

我的猜测是,你将UIBarButtonItem添加到错误的对象! 你需要把它添加到rootViewController(而不是你可能做到的UINavigationController

 YourRootViewController *theRootController = [[YourRootViewController alloc] init]; UINavigationController* navContainer = [[UINavigationController alloc] initWithRootViewController:theRootController]; UIButton* rightbutton = [UIButton buttonWithType:UIButtonTypeCustom]; [rightbutton setBackgroundImage:[UIImage imageNamed:@"share-icon.png"] forState:UIControlStateNormal]; [rightbutton addTarget:self action:@selector(share:) forControlEvents:UIControlEventTouchUpInside]; theRootController.navigationItem.rightBarButtonItem = rightbutton; [navContainer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve]; [self presentModalViewController:navContainer animated:YES]; 

如果您使用的是白色导航栏,请不要忘记在UIBarButtonItem上设置tintColor。 我的button在那里,但无形。

使用swift 3.0 iOS 10.0

让customButton = UIBarButtonItem(title:“Right”,style:.plain,target:self,action:#selector(yeah))customButton.width = 100.0 navigationItem.rightBarButtonItem = customButton

func是啊(发件人:UIBarButtonItem){打印(“哟你的男人/女人”)}