0

我想显示图像和标题,RightBar Button所以我编写了如下代码:

    UIBarButtonItem *negativeSpacer = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil] autorelease];
    negativeSpacer.width = BARBUTTONITEM_PADDING_IPAD;
    UIImage *buttonImage = [UIImage imageNamed:@"select-ipad@3x.png"];
    UIButton *selectButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [selectButton setImage:buttonImage forState:UIControlStateNormal];
    [selectButton setTitle:@"Select" forState:UIControlStateNormal];
    [selectButton setTitleColor:[UIColor colorWithRed:0.76 green:0.21 blue:0.08 alpha:1.0] forState:UIControlStateNormal];
    [selectButton.titleLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:14.0]];
    [selectButton sizeToFit];
    UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:selectButton];
    [selectButton addTarget:self action:@selector(didSelectButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:negativeSpacer,aBarButtonItem,nil]];

此代码显示标题和图像NavigationBar,当我单击它时,单击的喜欢仅出现在图像上,而不出现在文本上。

4

2 回答 2

0
-(void)viewWillAppear:(BOOL)animated{    
UIButton *btn=[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 25, 25)];
[btn setImage:[UIImage imageNamed:@"select-ipad@3x.png"] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(onBtnRightClick:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *btnRight=[[UIBarButtonItem alloc]initWithCustomView:btn];
self.navigationItem.leftBarButtonItem = btnRight;      
}

-(IBAction)onBtnRightClick:(id)sender
{
    NSLog(@"Right Bar Button Clicked.");
}
于 2016-06-06T11:34:04.763 回答
0

使用您的代码,尝试 2 个按钮:

UIButton *button1=[UIButton buttonWithType:UIButtonTypeCustom];
[button1 setFrame:CGRectMake(0.0, 0.0, 45.0, 45.0)];
[button1 addTarget:self action:@selector(didSelectButton:) forControlEvents:UIControlEventTouchUpInside];
[button1 setImage:[UIImage imageNamed:@"select-ipad@3x.png"] forState:UIControlStateNormal];

UIBarButtonItem *buttonItem1 = [[UIBarButtonItem alloc]initWithCustomView:button1];

UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
[button2 addTarget:self action:@selector(didSelectButton:) forControlEvents:UIControlEventTouchUpInside];
[button2 setTitle:@"Select" forState:UIControlStateNormal];
[button2 setTitleColor:[UIColor colorWithRed:0.76 green:0.21 blue:0.08 alpha:1.0] forState:UIControlStateNormal];
[button2 setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
[button2.titleLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:14.0]];
[button2 sizeToFit];

UIBarButtonItem *buttonItem2 = [[UIBarButtonItem alloc] initWithCustomView:button2];

self.navigationItem.rightBarButtonItems = @[buttonItem2,buttonItem1];

我希望这可以帮助你。

于 2016-06-06T11:36:47.040 回答