我正在尝试获取 UIButton
作为 UINavigationBar
的标题 View (在 UINavigationController
中),但是当向按钮似乎拉伸(stretch)了整个标题 View :
我是不是做错了什么或者这是一个错误?有解决方法吗?我正在运行 iOS 11 beta 2,所以它可能只是一个测试版错误,但无法在 iOS10 上测试。任何帮助将不胜感激! :)
代码:
let titleButton = UIButton(type: .system)
titleButton.sizeToFit()
titleButton.tintColor = UFOGREENTHEMECOLOR
titleButton.setTitleColor(BLACKTEXTCOLOR, for: .normal)
titleButton.setTitle("John Doe", for: .normal)
titleButton.titleLabel?.font = UIFont(name: "Avenir-Book", size: 20)
titleButton.setImage(UIImage(named: "Default Avatar.png"), for: .normal)
self.navigationItem.titleView = titleButton
编辑: 在 IOS10 上试过,同样的行为。添加 titleButton.imageView?.contentMode = .scaleAspectFit
正如 CoderAzreal 所建议的那样似乎修复了被拉伸(stretch)的图像,但标题仍然偏向右侧。
最佳答案
试试 titleButton.imageView?.contentMode = .scaleAspectFit
关于ios - UIButton 作为 UINavigationBar 中的 titleview 奇怪的拉伸(stretch)错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44747066/