titleView
不居中的原因大部分情況下是因為titleview
的寬度超出了最大值,具體原因這篇文章iOS11.0設置導航欄self.navigationItem.titleView居中分析的極好,我這里主要提出我的解決方案,我認為更加簡潔,且適用性更強
titleView居中
//設置一個較小的寬度
let width = 44
navigationItem.titleView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: 44, height: 44))
//這是真正的目標titleView:
let naviContentView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: kScreenWidth, height: 44))
//naviContentView addSubViews
//設置center
naviContentView.center = CGPoint.init(x: 22, y: 22)
navigationItem.titleView?.addSubview(naviContentView)