如何设置navigationItem.titleView图片水平居中?

How to set navigationItem.titleView image horizontal center?

你好,我在 im 中有一个简单的项目,在 UIImageView 徽标中使用 UINavigationBar,但有些视图看起来是 30-40px 右侧,有些视图看起来是 50-60px 左侧?

如何设置navigationItem.titleView图片水平居中?

下面是我的代码。

        navigationController!.navigationBar.barTintColor = UIColor.clearColor()
        navigationController!.navigationBar.tintColor = UIColor.whiteColor();

        let imageView = UIImageView(frame: CGRect(x: 50, y: 0, width: 164, height: 38))
        imageView.contentMode = .ScaleAspectFit
        let image = UIImage(named: "logo.png")
        imageView.image = image
        navigationItem.titleView = imageView

输出图片;

另外,当我将 x:50 更改为 x:80x:0 等时,什么也没发生!

你可以像这样设置x原点0

let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 164, height: 38))