【发布时间】:2017-09-27 21:24:10
【问题描述】:
我正在尝试为带有“标题”和“描述”标签的导航控制器实现自定义 titleView。如果我把这个 titleView 放在第一个 VC 上,它看起来不错。如果我将第二个 VC 推入导航堆栈并弹出它,titleView 的位置就会改变。
titleView 的约束:
titleLabel.translatesAutoresizingMaskIntoConstraints = false
descriptionLabel.translatesAutoresizingMaskIntoConstraints = false
titleLabel.widthAnchor.constraint(equalTo: widthAnchor).isActive = true
descriptionLabel.widthAnchor.constraint(equalTo: titleLabel.widthAnchor).isActive = true
titleLabel.topAnchor.constraint(equalTo: topAnchor).isActive = true
descriptionLabel.bottomAnchor.constraint(equalTo: bottomAnchor, constant: 2.0).isActive = true
在 VC 的 viewDidLoad 中,我使用以下代码插入 titleView:
navigationItem.titleView = BarTitleView()
navigationItem.titleView?.bounds = CGRect(x: 0, y: 0, width: view.bounds.width, height: 44)
navigationItem.titleView?.updateConstraints()
我尝试在viewWillAppear 中插入后续行(第二个 VC 有不同的条形按钮,这可能是问题的根源),但没有任何改变
navigationItem.titleView?.bounds = CGRect(x: 0, y: 0, width: view.bounds.width, height: 44)
navigationItem.titleView?.updateConstraints()
我该如何解决这个问题?
【问题讨论】: