开发者

How to Hide the UIbarbutton on navigation bar

开发者 https://www.devze.com 2022-12-24 20:29 出处:网络
hi i have created a bar button which displays Done when the editing of the text view starts. what actually i need that when i press the done button then the the editable property of textview will beco

hi i have created a bar button which displays Done when the editing of the text view starts. what actually i need that when i press the done button then the the editable property of textview will become false and the done button hides.

i 开发者_Python百科am done with the first part but how to hide the bar button?

please help..


@christo16's solution didn't quite work for me, but it lead me to this which worked:

[self.navigationItem.rightBarButtonItem setEnabled:NO];


You can disable it (it becomes dim):

//assuming it's the right one
[[[myNavigationBar topItem]rightBarButtonItem]setEnabled: NO];

I think to hide it, you actually have to set it nil. Then recreate it when you need it.

[[myNavigationBar topItem]setRightBarButtonItem:nil animated:NO];


If it was just a UIButton, I'd say you can do something like this:

myButton.hidden = YES;

But if it's a UIBarButtonItem, you could try the solution here. Let us know if it works!


Setting it enabled/disabled will not achieve the same as isHidden given that it will still be visible (just slightly dimmer). If you set the bar button item to nil, then you cannot easily toggle it back on again.

Instead, you can toggle visibility on/off by using tint color...here is a Swift implmentation.

Toggle off (simulating isHidden true):

        self.navigationItem.rightBarButtonItem.tintColor = UIColor.clear
        self.navigationItem.rightBarButtonItem.isEnabled = false

And back on (simulating isHidden false):

        self.navigationItem.rightBarButtonItem.tintColor = UIColor.white // or nil
        self.navigationItem.rightBarButtonItem.isEnabled = true
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号