我正在尝试在导航项中添加自定义lefBarButton 但它没有显示任何内容 我尝试了这些答案
https://stackoverflow.com/questions/48564480/custom-leftbarbuttonitem-not-showed-in-ios-11
navigation bar button not showing in swift 3
navigation bar button and items not showing in swift 3
Navigation bar button not showing
Bar button item not shown in navigationBar
Navigation bar not showing iOS swift
Swift 3 - Why is my Navigation Bar not showing?
iOS 8 Swift navigation bar title, buttons not showing in tab based application
我的 View Controller 的segue类型是show。
这是我的代码
let leftButton = UIButton(type: .roundedRect)
leftButton.addTarget(self, action: #selector(backClick), for: .touchUpInside)
leftButton.setTitle("", for: .normal)
leftButton.tintColor = UIColor.red
leftButton.imageView?.image = UIImage(named: "back-icon")
leftButton.frame = CGRect(x: 0, y: 0, width: 22, height: 15)
self.navigationItem.leftBarButtonItem = UIBarButtonItem.init(customView: leftButton)
我花了几个小时来修复它。 有没有人有任何想法。
最佳答案
问题出在这一行。
leftButton.imageView?.image = UIImage(named: "back-icon")
imageView is a read-only property.
如果你想设置一个图片,你应该这样设置。
leftButton.setImage(UIImage("back-icon"), for: .normal) // Make sure your image is actually named "back-icon"
关于ios - 自定义 leftBarButtonItem 未在 swift 4 中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51960704/