使用自动布局的 UIToolBar 布局约束问题

Posted

技术标签:

【中文标题】使用自动布局的 UIToolBar 布局约束问题【英文标题】:UIToolBar Layout Constraints Issue using Autolayout 【发布时间】:2020-05-08 15:27:20 【问题描述】:

此问题与 ios13 中的 UIToolBar 布局约束问题有关。我遇到以下布局约束警告,我正在使用自动布局:

[LayoutConstraints] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
    (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x600001018c30 h=--& v=--& _UIToolbarContentView:0x7fc8e4eacdf0.width == 0   (active)>",
    "<NSLayoutConstraint:0x600001003d40 H:|-(0)-[_UIButtonBarStackView:0x7fc8e4ead4d0]   (active, names: '|':_UIToolbarContentView:0x7fc8e4eacdf0 )>",
    "<NSLayoutConstraint:0x600001003cf0 H:[_UIButtonBarStackView:0x7fc8e4ead4d0]-(0)-|   (active, names: '|':_UIToolbarContentView:0x7fc8e4eacdf0 )>",
    "<NSLayoutConstraint:0x60000100e5d0 'TB_Leading_Leading' H:|-(16)-[_UIModernBarButton:0x7fc8e4f4e050'Add category']   (active, names: '|':_UIButtonBarButton:0x7fc8e4f4de80 )>",
    "<NSLayoutConstraint:0x60000102ec10 'TB_Trailing_Trailing' H:[_UIModernBarButton:0x7fc8e4f4e050'Add category']-(16)-|   (active, names: '|':_UIButtonBarButton:0x7fc8e4f4de80 )>",
    "<NSLayoutConstraint:0x6000010183c0 'UISV-canvas-connection' UILayoutGuide:0x600000a2a3e0'UIViewLayoutMarginsGuide'.leading == UIView:0x7fc8e4f0d920.leading   (active)>",
    "<NSLayoutConstraint:0x6000010185a0 'UISV-canvas-connection' UILayoutGuide:0x600000a2a3e0'UIViewLayoutMarginsGuide'.trailing == UIView:0x7fc8e4f4e8e0.trailing   (active)>",
    "<NSLayoutConstraint:0x6000010185f0 'UISV-spacing' H:[UIView:0x7fc8e4f0d920]-(0)-[_UIButtonBarButton:0x7fc8e4f4de80]   (active)>",
    "<NSLayoutConstraint:0x600001018640 'UISV-spacing' H:[_UIButtonBarButton:0x7fc8e4f4de80]-(0)-[UIView:0x7fc8e4f4e8e0]   (active)>",
    "<NSLayoutConstraint:0x6000010007d0 'UIView-leftMargin-guide-constraint' H:|-(0)-[UILayoutGuide:0x600000a2a3e0'UIViewLayoutMarginsGuide'](LTR)   (active, names: '|':_UIButtonBarStackView:0x7fc8e4ead4d0 )>",
    "<NSLayoutConstraint:0x600001001310 'UIView-rightMargin-guide-constraint' H:[UILayoutGuide:0x600000a2a3e0'UIViewLayoutMarginsGuide']-(0)-|(LTR)   (active, names: '|':_UIButtonBarStackView:0x7fc8e4ead4d0 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x60000102ec10 'TB_Trailing_Trailing' H:[_UIModernBarButton:0x7fc8e4f4e050'Add category']-(16)-|   (active, names: '|':_UIButtonBarButton:0x7fc8e4f4de80 )>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.

代码:

let toolBar: UIToolbar = 
    let tb = UIToolbar()
    tb.translatesAutoresizingMaskIntoConstraints = false
    return tb
()

fileprivate func setupView() 

    view.addSubview(toolBar)
    toolBar.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
    toolBar.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
    toolBar.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true

    toolBar.items = [
        UIBarButtonItem.flexibleSpace(),
        UIBarButtonItem(title: "Add category", style: .plain, target: self, action: #selector(addCategoryButtonTapped)),
        UIBarButtonItem.flexibleSpace()
    ]

正如post 和this 所建议的,手动指定工具栏的框架可以消除警告。但是,我无法让工具栏锚定到safeAreaLayoutGuide.bottomAnchor

尝试:

let toolBar = UIToolbar(frame: CGRect(x: 0, y: view.bounds.height - 50 - view.safeAreaInsets.bottom, width: view.bounds.width, height: 50))
view.addSubview(toolBar)

上面的代码没有将 toolBar 锚定在 safeAreaLayoutGuide 的底部,并且在旋转时消失。

更新:

尝试@Frankenstein 的解决方案可以解决设备不旋转时的问题。旋转后,警告再次浮出水面。见 gif:

【问题讨论】:

flexibleSpace()是你自己的函数吗? @Frankenstein 是的,为方便起见,它是我自己对 .flexibleSpace 按钮的扩展。 【参考方案1】:

UIToolbar 似乎工作得非常特别。需要先取消隐藏导航控制器的工具栏,然后设置ViewController的toolbarItems。代码如下:

class ViewController: UIViewController 

    override func viewDidLoad() 
        super.viewDidLoad()

        setupToolbar()
    

    func setupToolbar() 
        navigationController?.isToolbarHidden = false

        toolbarItems = [
            UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil),
            UIBarButtonItem(title: "Some Item", style: .plain, target: nil, action: nil),
            UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)]
    

【讨论】:

您的代码几乎可以正常工作。当设备没有旋转时,它会删除警告。但是,当设备旋转,然后导航到所述 ViewController 时,警告会再次出现。查看更新

以上是关于使用自动布局的 UIToolBar 布局约束问题的主要内容,如果未能解决你的问题,请参考以下文章

在自定义 UIView 类 Iphone 上应用自动布局约束。

ios 6自动布局约束错误

自定义 UITableViewCell 的自动布局约束在运行时失败

使用堆栈视图和自动布局创建自定义 UITableViewCell

在 UICollectionViewCell 中使用 Storyboard 中的自动布局约束

自定义 tableviewcell 动态高度未根据约束更新(以编程方式自动布局)