导航项按钮未显示 [英] navigationItem button not showing

查看:36
本文介绍了导航项按钮未显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UINavigationItem 我想显示一个 UIBarButtonItem.

I have a UINavigationItem i am trying to show a UIBarButtonItem.

现在的问题是我正确添加了它们,它们可以正常工作并且 100% 工作.

Now the problem is i added them correctly, they're functional and working 100%.

但它们没有显示在 UINavigationBar 上.

but they're not showing on the UINavigationBar.

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

Now for the flow I am doing the following.

1- 我隐藏了后退按钮,就像这样

1- I am hiding the back button, like this

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

2- 当用户点击 UIButton 时,我在运行时动态地使用函数添加这 2 个按钮.

2- I am adding those 2 buttons using a function dynamically at run time, when a user tap on a UIButton.

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- 我尝试使用这些函数,但结果仍然相同

3- I have tried to use those functions and still the same result

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

总结:

我尝试更改 tintColor,因为它们在功能上正常工作,我认为这是颜色问题.

I have tried to change the tintColor as they're functionally working i thought it was a color problem.

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

I have tried to use them inside DispatchQueue.main.async {} thinking it might be a thread problem since it dynamic.

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

I have debugged and check for the items in the UINavigationItem and they're exist.

主要发生了什么:

按钮没有显示,但在 UINavigationItem 上点击它们的位置时它们工作得很好.

The buttons are not shown but they are working just fine when tapped on their places on the UINavigationItem.

推荐答案

现在的问题是我正确添加了它们,它们可以正常工作并且工作 100%.

Now the problem is i added them correctly, they're functional and working 100%.

但它们没有显示在 UINavigationBar 上.

but they're not showing on the UINavigationBar.

根据您提到的内容,这只是一个 UI 问题 - 原因不明.

Based on what you mentioned, it is just a UI issue -for unknown reason-.

因此,您可以做的是确认导航项中将显示按钮项,即让栏按钮项具有自定义视图.添加 UIButton 作为 UIBarButtonItem 的自定义视图将是有效的,如下所示:

So what you could do to confirm that there are button items will be shown in the navigation item is to let the bar button item to has a custom view. Adding a UIButton as a custom view for UIBarButtonItem would be valid, like this:

// 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 更新,您可以编辑 rightButtonleftButton,例如:

Therefore, for any needed UI update, you could edit rightButton and leftButton, example:

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


此外,我认为没有必要调用:


In addition, I would assume that there is no need to call:

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

设置左栏按钮项时,默认应该替换后退按钮.

When setting the left bar button item, it should be a replacement for the back button by default.

这篇关于导航项按钮未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆