Swift 使用自动布局时UIToolBar布局约束问题

Swift 使用自动布局时UIToolBar布局约束问题,swift,Swift,此问题与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 c

此问题与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.
正如本文和所建议的,手动指定工具栏的框架可以删除警告。但是,我无法将工具栏定位到安全区域LayoutGuide.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)
上述代码不会将工具栏固定在safeAreaLayoutGuide的底部,并在旋转时消失

更新:

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


UIToolbar似乎特别有效。您需要首先取消隐藏导航控制器的工具栏,然后设置ViewController的工具栏项。代码如下:

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)]
    }
}

flexibleSpace是你自己的功能吗?@Frankenstein是的,为了方便起见,它是我自己对.flexibleSpace按钮的扩展。你的代码几乎可以工作。当设备没有旋转时,它确实会删除警告。但是,当设备旋转,然后导航到所述ViewController时,警告再次出现。请参阅更新
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)]
    }
}