我創建了具有中心垂直+中心水平約束的xib文件,稍後將作爲navigationItem的titleView。 如何從導航項中的xib自定義視圖中心titleView
裏面的xib的init我設置了文字和圖片。
class UIViewFromNib: UIView {
@IBOutlet var view: UIView!
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var titleImageView: UIImageView!
init(frame: CGRect,title:String,image:UIImage) {
super.init(frame: frame)
UINib(nibName: "NavTitle", bundle: nil)
.instantiate(withOwner: self, options: nil)
addSubview(view)
view.frame = self.bounds
titleLabel.text = title
titleImageView.image = image
}
}
然後,在videDidLoad中,我將視圖添加到navigationItem的titleView中。
let rect = CGRect(x: 0, y: 0, width: width, height: 40)
let width = self.view.bounds.width
let view = UIViewFromNib(frame: rect,
title: title,
image: image)
navigationItem.titleView = view
navigationItem.titleView?.sizeToFit()
在RTL更糟
我怎樣才能解決呢? 感謝