如何在导航栏的右上角显示图像?

时间:2010-10-09 04:40:41

标签: iphone xcode

我想在右侧角落的导航栏中显示图像。有人请告诉我怎么能这样做。我尝试在导航栏的右下角使用uiimageview但我没有得到它。谢谢提前

1 个答案:

答案 0 :(得分:0)

UINavigationItem(你想在这里使用的)有一个rightBarButtonItem,你可以设置。

做这样的事情:

UIBarButtonItem *rightCornerButton = [[UIBarButtonItem alloc] 
                initWithImage:[UIImage imageNamed:@"myAwesomeImage.png"] 
                style:UIBarButtonItemStylePlain target:self 
                action:@selector(functionYouWantToRunWhenBarButtonIsPressed)];
[self.navigationItem setRightBarButtonItem:rightCornerButton];
// If you do it this way, the button will disappear when you call -pushViewController on your navigationController.
[rightCornerButton release];

甚至更好(移除框架):

UIButton *barButton = [UIButton buttonWithType:UIButtonTypeCustom];
[barButton setImage:[UIImage imageNamed:@"reload1.png"] forState:UIControlStateNormal];
UIBarButtonItem *rightCornerButton = [[UIBarButtonItem alloc] initWithCustomView:barButton];
[rightCornerButton setTarget:self];
[rightCornerButton setAction:@selector(functionYouWantToRunWhenBarButtonIsPressed)];
[self.navigationItem setRightBarButtonItem:rightCornerButton];
// If you do it this way, the button will disappear when you call -pushViewController on your navigationController.
[rightCornerButton release];