iphone - 导航栏中的信息灯按钮

标签 iphone ios uibutton uinavigationbar

如何更改此代码,以便在导航栏的右上角显示一个信息按钮而不是一个单词?

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

相关文章:

ios - 在 Swift3 iOS 中使用字符串参数对对象数组进行排序

iphone - Objective c - 使用一个或多个 ViewController 在 View 之间翻转?

ios - 将时间戳从 Firebase 转换为 Time Ago

ios - 添加带有自定义图像的新 UIButton "state"

ios - 将 @objc 方法作为操作添加到按钮并获取 "Argument of ' #selector' 并不引用 '@objc' 方法、属性或初始值设定项”

iphone - 如何在分离的 UITableViewCell 和另一个 View 之间执行 segue

ios - 仅使用 iPhone 模拟器更好地测试 GPS 代码的巧妙方法?

iphone - iOS 下的 ZXing 不能在模拟器中运行

iphone - NSInvalidArgumentException与UITableView吗?

iphone - UIButton:设置选定突出显示状态的图像