ios - 在 rootView Swift 3.0 之后以编程方式在 ViewControllers 添加栏按钮项

标签 ios swift uinavigationcontroller uinavigationbar uinavigationitem

我有一个 View 层次结构

只有一个导航 Controller 和它的 rootViewController。

我想在圈出的 View 中添加一个左栏按钮项。

我要添加 barButton 的 View :

enter image description here

我试过了

self.navigationController?.navigationItem.leftBarButtonItem = editButtonItem

viewDidLoad()viewWillAppear() 中,但都没有成功。 我还尝试在 Storyboard中添加 UINavigationItem,然后添加 barButtonItem。这也不起作用。

编辑

我尝试了下面的答案,这就是我在输出中得到的结果。

尝试答案后的输出:

enter image description here

如有任何帮助,我们将不胜感激。

最佳答案

你可以像这样添加条形按钮

let btnleft : UIButton = UIButton(frame: CGRect(x:0, y:0, width:35, height:35))
btnleft.setTitleColor(UIColor.white, for: .normal)
btnleft.contentMode = .left

btnleft.setImage(UIImage(named :"yourimage.png"), for: .normal)
            btnleft.addTarget(self, action: #selector(YOUR_ACTION), for: .touchDown)
let backBarButon: UIBarButtonItem = UIBarButtonItem(customView: btnleft)

self.navigationItem.setLeftBarButtonItems([backBarButon], animated: false)

关于ios - 在 rootView Swift 3.0 之后以编程方式在 ViewControllers 添加栏按钮项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45413358/

相关文章:

ios - 点击时如何在 UICollectionViewCell 中制作此动画?

swift - 暂停和重新启动辅助同步线程

ios - 为什么弹出 UIViewController 会改变 navigationItem 的 titleTextAttributes?

ios - 从父类访问变量

ios应用程序随机崩溃

ios - 在uitableview中识别许多单元格

html - 使用静态 html、css 和 javascript 创建 Ipad 应用程序

ios - 需要一个 UINavigationController 的解决方案,带有应用程序的后退按钮

ios - 使用 drawRect : 设置 UIView backgroundColor

ios - 在 UIScrollView 的 subview 上使用手势识别器 - Swift