如何更改此代码,以便在导航栏的右上角显示一个信息按钮而不是一个单词?
- (void)viewDidLoad {
[self setTitle:@"About"];
[super viewDidLoad];
UIBarButtonItem *addButton = [[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"info", @"")
style:UIBarButtonItemStyleBordered
target:self
action:@selector(addAction:)] autorelease];
self.navigationItem.rightBarButtonItem = addButton;
}
最佳答案
这个问题经常被问到。
UIImage *buttonImage = [UIImage imageNamed:@"UIButtonInformationIcon.jpg"];
//create the button and assign the image
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
//set the frame of the button to the size of the image
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
[button addTarget:self action:@selector(anyActionYouWant) forControlEvents:UIControlEventTouchUpInside];
//create a UIBarButtonItem with the button as a custom view
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:button]autorelease];
这是几种解决方案中的一种。
关于iphone - 导航栏中的信息灯按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7009986/