0

Is it possible to hide UIBarButtonItem (rightButton of navigationBar) but not making it nil? In my application I have a condition

if(self.navigationItem.rightBarButtonItem == nil)  

which is really important. But hiding the barButton is equally important.

halfer
  • 19,824
  • 17
  • 99
  • 186
Nitish
  • 13,845
  • 28
  • 135
  • 263

3 Answers3

2

One thing you can do is use the initWithCustomView property of UIBarButtonItem. Set up a UIButton and use initWithCustomView and assign it to this button. UIButton has hiding and unhiding property.

UIButton*someButton=[UIButton UIButtonTypeRoundedRect];
UIBarButtonItem*someBarButton=[[UIBarButtonItem alloc] initWithCustomView:someButton];

//To Hide/Unhide
[someButton setHidden:YES/NO];
iNoob
  • 3,364
  • 2
  • 26
  • 33
1

You can hide your button by which you have made your barbutton ....

UIButton *button1=[UIButton buttonWithType:UIButtonTypeCustom];
[button1 setFrame:CGRectMake(10.0, 2.0, 45.0, 40.0)];
[button1 addTarget:self action:@selector(showLeft:) forControlEvents:UIControlEventTouchUpInside];
[button1 setImage:[UIImage imageNamed:@"anyImage.png"] forState:UIControlStateNormal];
UIBarButtonItem *button = [[UIBarButtonItem alloc]initWithCustomView:button1];
self.navigationItem.leftBarButtonItem = button;

[button1 setHidden:YES];
TheTiger
  • 13,264
  • 3
  • 57
  • 82
0

Please check my answer here to similar question. It's applicable here too.

It adds isHidden property to UIBarButtonItem.

Hange
  • 443
  • 3
  • 8