func setupRightNavigationItems()
{
let menuButton = UIButton(type: .system)
menuButton.setImage(#imageLiteral(resourceName: "menu-white").withRenderingMode(.alwaysOriginal),for:.normal)
menuButton.contentMode = .scaleAspectFit
menuButton.frame = CGRect(x:0,y:0,width:25,height:25)
let playButton = UIButton(type: .system)
playButton.setImage(#imageLiteral(resourceName: "nowplaying32").withRenderingMode(.alwaysOriginal), for: .normal)
playButton.contentMode = .scaleAspectFit
playButton.frame = CGRect(x:0,y:0,width:25,height:25)
let nowPlaying = defaults.string(forKey: "NOWPLAYING")
if nowPlaying == "true"
{
playButton.isHidden = false
}
else
{
playButton.isHidden = true
}
menuButton.addTarget(self, action: #selector(menuButtonClicked), for: UIControlEvents.touchUpInside)
playButton.addTarget(self, action: #selector(notificationPlayButtonClicked), for: UIControlEvents.touchUpInside)
navigationItem.rightBarButtonItems = [UIBarButtonItem(customView:menuButton),UIBarButtonItem(customView:playButton)]
}
当我调用此函数时,该按钮变为隐藏,即viewWillAppear中的setupRightNavigationItems,但它不反映按钮在视图中隐藏的变化,当我在检查条件后从另一个函数调用它时请帮助!。
答案 0 :(得分:0)
当你想隐藏
时self.navigationItem.rightBarButtonItem = nil
答案 1 :(得分:0)
您的代码运行良好。只是检查你的情况是否正确。
class DemoVC: UIViewController {
var flag = "true"
override func viewDidLoad() {
super.viewDidLoad()
setupRightNavigationItems()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
flag = "false"
setupRightNavigationItems()
}
@IBAction func onBtnChange(_ sender : UIButton) {
flag = "true"
setupRightNavigationItems()
}
func setupRightNavigationItems()
{
print("called")
let menuButton = UIButton(type: .system)
menuButton.setImage(#imageLiteral(resourceName: "likesong").withRenderingMode(.alwaysOriginal),for:.normal)
menuButton.contentMode = .scaleAspectFit
menuButton.frame = CGRect(x:0,y:0,width:25,height:25)
let playButton = UIButton(type: .system)
playButton.setImage(#imageLiteral(resourceName: "likesong").withRenderingMode(.alwaysOriginal), for: .normal)
playButton.contentMode = .scaleAspectFit
playButton.frame = CGRect(x:0,y:0,width:25,height:25)
let nowPlaying = "true"//defaults.string(forKey: "NOWPLAYING")
if nowPlaying.lowercased() == flag.lowercased()
{
playButton.isHidden = true
}
else
{
playButton.isHidden = false
}
self.navigationItem.rightBarButtonItems = [UIBarButtonItem(customView:menuButton),UIBarButtonItem(customView:playButton)]
}
}
我从viewDidLoad
调用此函数并且正常工作。我只是为测试添加了静态条件。
在touchesBegan
按钮上显示,onBtnChange
再次隐藏。
试试这个,希望这项工作。
答案 2 :(得分:0)
您可以执行以下操作来隐藏和显示导航按钮
<强>隐藏强>
self.navigationItem.rightBarButtonItem?.tintColor = .clear
self.navigationItem.rightBarButtonItem?.isEnabled = false
显示:强>
self.navigationItem.rightBarButtonItem?.tintColor = yourbuttonTint color
self.navigationItem.rightBarButtonItem?.isEnabled = true
答案 3 :(得分:0)
你是对的,当从willAppear
或didLoad
以外的功能调用时,它不会隐藏,因为两个条形按钮项都已创建。你不能简单地隐藏它,
所以在调用此函数之前,请删除rightBarButtonItem
,现在每件事都可以。
试试这个,
@IBAction func hideNavigationBar(_ sender: UIButton) {
navigationItem.rightBarButtonItems = nil
setupRightNavigationItems()
}
试试这个
let nowPlaying = defaults.string(forKey: "NOWPLAYING")
if nowPlaying == "true"
{
navigationItem.rightBarButtonItems = [UIBarButtonItem(customView:menuButton),UIBarButtonItem(customView:playButton)]
}
else
{
navigationItem.rightBarButtonItems = [UIBarButtonItem(customView:menuButton)]
}
<强>结果:强>
答案 4 :(得分:0)
首先尝试设置nil,然后将按钮设置为刷新视图。
func setupRightNavigationItems() {
// here your rest code
// set nil here >>>
navigationItem.rightBarButtonItems = nil
navigationItem.rightBarButtonItems = [UIBarButtonItem(customView:menuButton),UIBarButtonItem(customView:playButton)]
}