ios - 自定义 leftBarButtonItem 未在 swift 4 中显示

标签 ios swift uinavigationbar

我正在尝试在导航项中添加自定义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)

我花了几个小时来修复它。 有没有人有任何想法。

enter image description here

这里是后退按钮 enter image description here

最佳答案

问题出在这一行。

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/

相关文章:

ios - AF网络 : Send image from file

objective-c - 为什么 performSegueWithIdentifier 在 vi​​ewDidLoad 中不起作用?

ios - UIScrollView 在更新约束 subview 时表现得很奇怪

xcode - 如何从 Storyboard 中删除特定的 Size Class?

ios - 导航栏的横向模式问题

ios - UIScrollView-zoomToRect :animated: and CGPoint conversion between views

javascript - 用户删除并重新安装应用程序后管理 Parse _Installation 类

iphone - 导航栏中的按钮颜色 - iPhone

ios - 如何更改 MFMessageComposeViewController 的导航栏颜色?

ios - WKWebView - iframe 内容未加载到模拟器/设备中