最佳答案
可能的解决方案。您可以创建 UIBarButton 项目的扩展。 示例:
import UIKit
extension UIBarButtonItem {
convenience init (image: UIImage?, target: Any?, action: Selector?) {
let button = UIButton(type: .custom)
button.imageView?.contentMode = .scaleAspectFit
button.frame = CGRect(x: 0.0, y: 0.0, width: 44.0, height: 44.0)
button.addTarget(target, action: action!, for: .touchUpInside)
button.setImage(image, for: UIControlState.normal)
button.imageEdgeInsets = UIEdgeInsets(top: 7.0, left: 7.0, bottom: 7.0, right: 7.0)
if #available(iOS 9.0, *) {
button.widthAnchor.constraint(equalToConstant: 44.0).isActive = true
button.heightAnchor.constraint(equalToConstant: 44.0).isActive = true
}
self.init(customView: button)
}
}
然后在您的 viewDidLoad
方法中使用它:
let rightButton = UIBarButtonItem(image: UIImage(named: "example"), target: self, action: #selector(exampleAction(sender:)))
self.navigationItem.rightBarButtonItem = rightButton
关于ios - 如何以编程方式插入右键图像 UINavigationbar swift 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48316280/