仅限 IOS 11 : Navigation bar label off on top
<p><p>IOS 11 导致主标签从顶部移动一点,而不是保持在顶部。该问题仅出现在 IOS 11 上。使用不同的 IOS,一切看起来都很好。 </p>
<p>带有设置标题的代码示例:</p>
<pre><code> private func setHeader(agentName: String = "", isTyping: Bool = false) -> Void {
if (agentName.isEmpty) {
self.containerViewController?.navigationItem.titleView = nil
} else {
let headerView: UIView = {
let rect = CGRect(x: 0, y: 0, width: 320, height: 44)
let uiview = UIView(frame: rect)
return uiview
}()
let headerLabel: UILabel = {
let label = UILabel(frame: CGRect(x: 0, y: 0, width: headerView.frame.width, height: 20))
label.font = UIFont.systemFont(ofSize: 18)
label.textAlignment = .center
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
let subheaderLabel: UILabel = {
let label = UILabel(frame: CGRect(x: 0, y: 0, width: headerView.frame.width, height: 12))
label.font = UIFont.systemFont(ofSize: 12)
label.translatesAutoresizingMaskIntoConstraints = false
label.text = self.title
return label
}()
headerView.addSubview(headerLabel)
headerView.addSubview(subheaderLabel)
let viewsDictionary = ["header": headerLabel, "subheader": subheaderLabel]
headerView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:||", options: NSLayoutFormatOptions(), metrics: nil, views: viewsDictionary))
headerView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:||", options: NSLayoutFormatOptions(), metrics: nil, views: viewsDictionary))
headerView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-", options: [], metrics: nil, views: viewsDictionary))
self.containerViewController?.navigationItem.titleView = headerView
}
}
</code></pre>
<p> <a href="/image/ujkEm.png" rel="noreferrer noopener nofollow"><img src="/image/ujkEm.png" alt="Label off"/></a> </p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>从 iOS 11 开始,添加到工具栏的 View 现在使用自动布局进行布局。您应该在 <code>headerView</code> 上添加大小限制。例如:</p>
<pre><code>headerView.widthAnchor.constraintEqualToConstant(320.0).isActive = true
headerView.heightAnchor.constraintEqualToConstant(44.0).isActive = true
</code></pre>
<p>否则,自动布局将使用您的标题 View 的内在内容大小,这可能不是您所期望的。 </p>
<p>有关更多信息,请参阅 WWDC 2017session<a href="https://developer.apple.com/videos/play/wwdc2017/204/" rel="noreferrer noopener nofollow">Updating your app for iOS 11</a> .</p></p>
<p style="font-size: 20px;">关于仅限 IOS 11 : Navigation bar label off on top,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/46793954/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/46793954/
</a>
</p>
页:
[1]