Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/112.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios navigationItem按钮未显示_Ios_Swift_Uinavigationitem - Fatal编程技术网

Ios navigationItem按钮未显示

Ios navigationItem按钮未显示,ios,swift,uinavigationitem,Ios,Swift,Uinavigationitem,我有一个UINavigationItem我正试图显示一个UIBarButtonItem 现在的问题是我正确地添加了它们,它们功能齐全,100%正常工作 但是它们没有显示在UINavigationBar上 现在,对于流程,我将执行以下操作 1-我把后退按钮藏起来了,就像这样 self.navigationItem.setHidesBackButton(isBackHidden, animated: false) 2-当用户点击ui按钮时,我在运行时使用函数动态添加这两个按钮 let rightB

我有一个
UINavigationItem
我正试图显示一个
UIBarButtonItem

现在的问题是我正确地添加了它们,它们功能齐全,100%正常工作

但是它们没有显示在
UINavigationBar

现在,对于流程,我将执行以下操作

1-我把后退按钮藏起来了,就像这样

self.navigationItem.setHidesBackButton(isBackHidden, animated: false)
2-当用户点击
ui按钮时,我在运行时使用函数动态添加这两个按钮

let rightBarItem = UIBarButtonItem(title: "Button.Done".localized, style: .plain, target: self, action: #selector(self.saveButtonTapped))
navigationItem.rightBarButtonItem = rightBarItem     
let leftBarItem = UIBarButtonItem(title: "Button.Cancel".localized, style: .plain, target: self, action: #selector(self.cancelButtonTapped))
navigationItem.rightBarButtonItem?.tintColor = .red // still nothing    
navigationItem.leftBarButtonItem = leftBarItem
3-我尝试过使用这些函数,但仍然得到相同的结果

self.navigationItem.setLeftBarButton(UIBarButtonItem?, animated: Bool)
self.navigationItem.setRightBarButton(UIBarButtonItem?, animated: Bool)
摘要

我曾尝试更改
色彩
,因为它们在工作,我认为这是一个颜色问题

我尝试在
DispatchQueue.main.async{}
中使用它们,认为这可能是一个线程问题,因为它是动态的

我已经调试并检查了
UINavigationItem
中的项,它们已经存在

主要发生了什么:

按钮未显示,但在
UINavigationItem
上点击按钮位置时,它们工作正常

现在的问题是我正确地添加了它们,它们功能齐全而且 工作100%

但是他们没有在UINavigationBar上出现

根据你提到的,这只是一个UI问题,原因不明

因此,要确认导航项中是否会显示按钮项,可以让工具栏按钮项具有自定义视图。添加
ui按钮
作为
UIBarButtonItem
的自定义视图是有效的,如下所示:

// right
let rightButton = UIButton(type: .custom)
rightButton.setTitle("Button.Done".localized, for: .normal)
rightButton.addTarget(self, action: #selector(saveButtonTapped), for: .touchUpInside)
let rightBarButtonItem = UIBarButtonItem(customView: rightButton)
navigationItem.setRightBarButton(rightBarButtonItem, animated: true)

// left
let leftButton = UIButton(type: .custom)
leftButton.setTitle("Button.Cancel".localized, for: .normal)
leftButton.addTarget(self, action: #selector(cancelButtonTapped), for: .touchUpInside)
let leftBarButtonItem = UIBarButtonItem(customView: leftButton)
navigationItem.setLeftBarButton(leftBarButtonItem, animated: true)
因此,对于任何需要的UI更新,您可以编辑
rightButton
leftButton
,例如:

leftButton.layer.backgroundColor = UIColor.red.cgColor
leftButton.layer.borderWidth = 2.0
leftButton.layer.borderColor = UIColor.black.cgColor

此外,我认为无需致电:

self.navigationItem.setHidesBackButton(isBackHidden, animated: false)

设置左栏按钮项目时,默认情况下它应该是后退按钮的替代品。

因此,您正试图在运行时基于特定事件添加“左”栏按钮项目,对吗?是的,左和右,@AhmadFI尝试了您的第二个代码段,对我来说效果很好。。。你认为有什么额外的信息值得一提吗?@AhmadF他们在点击navItem时工作,但没有显示。。。。。。bde amoslk,它起作用了。。你能不能把它贴出来,解释一下问题的原因和原因