导航栏中的信息灯按钮

时间:2011-08-10 11:24:33

标签: 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;  
}

2 个答案:

答案 0 :(得分:2)

经常有人提出这个问题。

        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];  

这是一个解决方案,其中有几个。

答案 1 :(得分:0)

或者......您可以在向标题栏添加按钮时从下拉列表中选择所需的按钮类型。您需要选择“信息灯”类型。