ios - 导航标题 UIImageView 无法居中

标签 ios swift uinavigationcontroller uiimageview

我有一个UIViewController,我从互联网加载一张图片,我想将其显示为导航标题 View 。

这是我从互联网获取图片后的代码

代码片段:

let imgView = UIImageView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
imgView.backgroundColor = UIColor(red: 0.5, green: 0.5, blue: 0, alpha: 0.3)
imgView.image = result.image!  //Contains the picture from internet
imgView.contentMode = .ScaleAspectFit
self.navigationItem.titleView = imgView

结果如下:

titleView not centered

最佳答案

好的,我找到原因了。

所以 UIImageView 确实位于 titleView 的中心。 问题是 titleView 未居中,因为在我的情况下,左栏按钮项目比右栏按钮项目大。

我必须将两个栏按钮项目设置为相同的宽度,现在我的 titleView 居中。

titleView centered

关于ios - 导航标题 UIImageView 无法居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39592977/

相关文章:

ios - UITableview 从上一个 View 返回时更改 Y 位置

iphone - 使用 UINavigationController 传递数据

ios - prepareForSegue 没有发送正确的参数

xcode - 来自 iAd 的插页式广告无法在 swift 上运行

ios - UICollectionView x3 在标题 Collection View 中彼此创建 Collection View slider

ios - Storyboard - 对象内部的对象作为 IBOutlet

ios - 具有相同 Root View 的导航 Controller

ios - 向 UINavigationController 添加自定义 View

ios - 如何以相同的方式更改 NSMutablearray 的顺序另一个可变数组被更改

ios - 代码 : Cannot invoke initializer for type 'String' with an argument list of type '(CGFloat)'