Пользовательский вид в большой UINavigationBar Использование ограничений на максимальный размер - PullRequest
3 голосов
/ 21 марта 2019

Я создаю пользовательское представление, которое помещаю в качестве подпредставления внутри UINavigationBar в viewDidLoad.Цель состоит в том, чтобы изменить размер панели навигации и изменить высоту этого вида, но она не будет расширяться за пределы исходного размера панели навигации, когда она была загружена.Все это работает, за исключением того, что оно не будет ограничивать высоту представления.

Как получить ограничение, чтобы ограничить максимальную высоту представления, чтобы иметь приоритет над ограничениями лид / трейлинг?

override func viewDidLoad() {
    super.viewDidLoad()
    if let navigationBar = self.navigationController?.navigationBar, let titleView = self.titleView {
        navigationBar.addSubview(titleView)

        let heightConstraint = titleView.heightAnchor.constraint(lessThanOrEqualToConstant: navigationBar.bounds.height)
        heightConstraint.priority = .required
        heightConstraint.isActive = true

        let topConstraint = titleView.topAnchor.constraint(equalTo: navigationBar.topAnchor)
        topConstraint.priority = .required
        topConstraint.isActive = true

        let horizontalConstraints = NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[titleView]-0-|", options: .directionLeadingToTrailing, metrics: nil, views: ["titleView": titleView])
        for constraint in horizontalConstraints {
            constraint.priority = .required
            constraint.isActive = true
        }

        let verticalConstraints = NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[titleView]-0-|", options: .directionLeadingToTrailing, metrics: nil, views: ["titleView": titleView])
        for constraint in verticalConstraints {
            constraint.priority = .fittingSizeLevel
            constraint.isActive = true
        }
    }
}

Почему ограничение heightAnchor не имеет приоритета?

...