2
有成千上萬的解釋在導航欄中設置圖像,但恕我直言,他們都有同樣的問題:他們使用固定高度的UIImageView
,但如果你旋轉設備navigationBar
- 高度減小的大小和圖像太高。我試圖用約束來解決這個問題,但我失敗了:如何在navigationBar中正確設置圖片的約束條件?
let imageView = UIImageView(image: UIImage(named: "logo.png"))
imageView.contentMode = .scaleAspectFit
self.navigationItem.titleView = imageView
let height = NSLayoutConstraint(
item: imageView, attribute: .height,
relatedBy: .equal,
toItem: self.navigationItem.titleView, attribute: .height,
multiplier: 1, constant: 0
)
NSLayoutConstraint.activate([ height ])
它在橫向模式下不起作用!