0

I want to show image and the title on RightBar Button so i have written code as below:

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

This code shows the title and image on the NavigationBar and when i click on it, then the clicked like appears only on the image but not on the text.

Uzair Dhada
  • 333
  • 2
  • 6
  • 23

2 Answers2

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.");
}
0

Using your code, try 2 buttons:

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

I hope this can help you.

J. Lopes
  • 1,336
  • 16
  • 27
  • @uzairdhada Let me know any updates, I've tested and worked for me. – J. Lopes Jun 06 '16 at 22:40
  • it works fine, but the case is that both does not gets a click look at the same time, what i want is that if user click on image or on the text the whole should be click like look. – Uzair Dhada Jun 07 '16 at 11:45
  • @uzairdhada ow I get it. I thought would change that. So you can use your code and try add this to your `select` button `[aBarButtonItem setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];` – J. Lopes Jun 07 '16 at 14:21