xcode 在 iOS-7 中按需隐藏和显示左侧导航栏按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25087585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
hide and show left navigation bar button on demand in iOS-7
提问by learner
I added my left navigation bar button using the storyboard. but I want it to hide when I first load the screen. And then in response to something else, I want it to show. The navigation bar has a method for hiding the back button. But there is no method for hiding/showing the left button. Is there a simple way for doing this? Or do I have to use two methods: the hiding method creates an empty button and the showing method creates the correct button? The button in question is just the Add
template that iOS provides (which makes it easy to just use the one in the storyboard than to create my own).
我使用情节提要添加了左侧导航栏按钮。但我希望它在我第一次加载屏幕时隐藏。然后为了回应其他事情,我希望它显示出来。导航栏有隐藏后退按钮的方法。但是没有隐藏/显示左按钮的方法。有没有一种简单的方法可以做到这一点?或者我必须使用两种方法:隐藏方法创建一个空按钮,显示方法创建正确的按钮?有问题的按钮只是Add
iOS 提供的模板(这使得在故事板中使用该按钮比创建我自己的更容易)。
回答by learner
Here is how I solved it
这是我解决它的方法
-(void) hideAndDisableRightNavigationItem
{
[self.navigationItem.rightBarButtonItem setTintColor:[UIColor clearColor]];
[self.navigationItem.rightBarButtonItem setEnabled:NO];
}
-(void) showAndEnableRightNavigationItem
{
[self.navigationItem.rightBarButtonItem setTintColor:[UIColor blackColor]];
[self.navigationItem.rightBarButtonItem setEnabled:YES];
}
回答by LHIOUI
Swift version of @learner answer
@learner 答案的 Swift 版本
func hideAndDisableRightNavigationItem (){
self.navigationItem.rightBarButtonItem?.enabled = false
self.navigationItem.rightBarButtonItem?.tintColor = UIColor.clearColor()
}
func showAndEnableRightNavigationItem(){
self.navigationItem.rightBarButtonItem?.enabled = true
self.navigationItem.rightBarButtonItem?.tintColor = UIColor. blackColor()
}
回答by Nex Mishra
Here is what I did. On the initial screen I wanted to hide the navigation bar:
这是我所做的。在初始屏幕上,我想隐藏导航栏:
self.navigationController.navigationBarHidden = YES;
On the second screen I wanted to show the navigation bar so I set:
在第二个屏幕上我想显示导航栏,所以我设置:
self.navigationController.navigationBarHidden = NO;