As ndmeiri mentioned
the bar button item expects the specified custom view to handle any
user interactions
And this is how you do it:
UIImageView *navView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"notification_alert.png"]];
navView.userInteractionEnabled = YES;
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:navView];
UITapGestureRecognizer *navViewTapRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(btnClick:)];
[navViewTapRecognizer setNumberOfTouchesRequired:1];
[navView addGestureRecognizer:navViewTapRecognizer];
navView.userInteractionEnabled = YES;
The action :
-(void)btnClick:(id)sender
{
NSLog(@"nav button clicked");
}
But it's better to just set a customized UIButton
as a UIBarButtonItem
And this is how you do it :
UIButton *myButton = [[UIButton alloc] init];
myButton.frame=CGRectMake(0,0,40,40);
[myButton setBackgroundImage:[UIImage imageNamed: @"notification_alert.png"] forState:UIControlStateNormal];
[myButton addTarget:self action:@selector(BtnClick:) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:myButton];
Also you can setup an image like this :
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"notification_alert.png"]
style:UIBarButtonItemStylePlain
target:self
action:@selector(btnClick:)];