我需要隐藏导航栏中的右侧按钮,然后在用户选择某些选项后再显示它。
不幸的是,下面的代码无效:
NO GOOD: self.navigationItem.rightBarButtonItem.hidden = YES; // FOO CODE
有没有办法?
我需要隐藏导航栏中的右侧按钮,然后在用户选择某些选项后再显示它。
不幸的是,下面的代码无效:
NO GOOD: self.navigationItem.rightBarButtonItem.hidden = YES; // FOO CODE
有没有办法?
对于Swift 5,隐藏rightBarButtonItem的方法
self.navigationItem.rightBarButtonItem?.customView?.isHidden = true
self.navigationItem.rightBarButtonItem?.image = nil
self.navigationItem.rightBarButtonItem?.isEnabled = false
Swift 2:
技巧!
隐藏:
if let btn = self.tabBarController!.navigationItem.rightBarButtonItem {
btn.enabled = false
btn.title = ""
}
展示:
if let btn = self.tabBarController!.navigationItem.rightBarButtonItem {
btn.enabled = true
btn.title = "ButtonName"
}
@IBOutlet var navigationItemButton: UIBarButtonItem!
Step 2: 创建隐藏按钮函数:
func hideNavigationButton() {
navigationItemButton.isEnabled = false
navigationItemButton.tintColor = UIColor.clear
}
Step 3: Create Show button function:
func showNavigationButton() {
navigationItemButton.isEnabled = true
navigationItemButton.tintColor = UIColor.white
}
步骤四:只需调用您想要的函数,使用hideNavigationButton()
来隐藏按钮,使用showNavigationButton()
来显示按钮。
祝好!
显示:
//set navigationItem tint color white
self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
隐藏:
//set navigationItem tint clear white
self.navigationItem.rightBarButtonItem.tintColor = [UIColor clearColor];
将标题先设为空,然后在选择后再进行设置。
隐藏:
if let topItem = self.navigationController?.navigationBar.topItem {
topItem.rightBarButtonItem = nil
}