ios - UIButton 作为 UINavigationBar 中的 titleview 奇怪的拉伸(stretch)错误?

标签 ios uinavigationcontroller uibutton uinavigationbar

我正在尝试获取 UIButton 作为 UINavigationBar 的标题 View (在 UINavigationController 中),但是当向按钮似乎拉伸(stretch)了整个标题 View :

enter image description here

我是不是做错了什么或者这是一个错误?有解决方法吗?我正在运行 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/

相关文章:

ios - Swift,如何告诉 Controller 另一个 Controller 是它的委托(delegate)

swift - 位于 View 顶部的导航栏

iphone - UINavigationController 并呈现模态 Controller

ios - 在 UIButton 中使用哪种内容模式来缩放图像?

ios - 设置 DLRadioButton 图标和标题之间的 marginWidth

ios - 单击 UIButton 时 UITableView 卡住

iphone - 关于 block 中对象的 Apple 文档

ios - 使用 iOS 'Protected Unless Open' 加密关闭文件时会发生什么?

iphone - UIPickerView 在选择器中消失的图像

ios - UINavigationController 和后退按钮操作