简体   繁体   中英

Navigation title UIImageView can't center

I have a UIViewController where I load a picture from internet and I want to display it as the navigation titleView.

Here is my code when I have finished fetching the picture from internet

Code Snippet :

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

And here is the result:

titleView未居中

Okay I found out the reason.

So the UIImageView is indeed centered in the titleView. The problem is that the titleView is not centered because in my case the left bar button item was bigger than the right bar button item.

I had to set both bar button items at the same width and now my titleView is centered.

titleView居中

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM