【问题标题】:Autolayout error - Unable to simultaneously satisfy constraints自动布局错误 - 无法同时满足约束
【发布时间】:2016-01-13 05:09:30
【问题描述】:

这个问题与我之前提出的关于如何添加在单独的 xib here 中定义的视图的问题有关。我仍在为此苦苦挣扎,现在我遇到了这个布局特定的错误,所以我认为最好提出一个新问题。

我有 2 个 xib,我正在尝试将来自 xib1 的视图插入 xib2 上的 NSView 占位符,并且我希望在调整视图大小时调整来自 xib1 的视图的大小。

这是我尝试设置左/右约束的代码。

    let placeholder: NSView = self.m_viewPlaceHolder;
    let insert: NSView = UserView01().view;
    insert.frame = NSRect(x: 0, y: 0, width: placeholder.frame.width, height: placeholder.frame.height);
    placeholder.addSubview(insert);

    NSUserDefaults.standardUserDefaults().setBool(true, forKey: "NSConstraintBasedLayoutVisualizeMutuallyExclusiveConstraints");
    let hor = NSLayoutConstraint.constraintsWithVisualFormat("H:|-[view]-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["view" : insert]);
    NSLayoutConstraint.activateConstraints(hor);

这是我在跟踪中看到的错误

2016-01-12 20:49:09.307 SubView01[9240:308241] Unable to simultaneously satisfy constraints:
(
"<NSLayoutConstraint:0x608000087800 H:|-(NSSpace(20))-[NSView:0x608000120500]   (Names: '|':NSView:0x6080001206e0 )>",
"<NSAutoresizingMaskLayoutConstraint:0x6000000842e0 h=--& v=&-- H:|-(0)-[NSView:0x608000120500]   (Names: '|':NSView:0x6080001206e0 )>"
)

Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x608000087800 H:|-(NSSpace(20))-[NSView:0x608000120500]   (Names: '|':NSView:0x6080001206e0 )>

Set the NSUserDefault NSConstraintBasedLayoutVisualizeMutuallyExclusiveConstraints to YES to have -[NSWindow visualizeConstraints:] automatically called when this happens.  And/or, break on objc_exception_throw to catch this in the debugger.
2016-01-12 21:00:19.409 SubView01[9240:308241] Clicked on overlapping visualized constraints: (
"<NSLayoutConstraint:0x608000087800 H:|-(NSSpace(20))-[NSView:0x608000120500]   (Names: '|':NSView:0x6080001206e0 )> (Actual Distance - pixels):0",
"<NSAutoresizingMaskLayoutConstraint:0x6000000842e0 h=--& v=&-- H:|-(0)-[NSView:0x608000120500]   (Names: '|':NSView:0x6080001206e0 )> (Actual Distance - pixels):0"
)

我在设计器中添加的唯一约束是主窗口占位符和用户视图标签上的左/上/右/下 0 像素约束。

【问题讨论】:

    标签: macos cocoa autolayout swift2


    【解决方案1】:

    与视图的隐式自动调整大小掩码存在冲突。

    两种选择:

    1. 通过关闭 AutoLayout、更改掩码并再次打开 AutoLayout 或取消选中 translates autoresizing mask into constraints(如果可用),将掩码设置为适合 Interface Builder 中的内容。

    2. 以编程方式执行相同的操作,或者设置掩码

      view.autoresizingMask = [...]
      

      或禁用translatesAutoresizingMaskIntoConstraints

      view.translatesAutoresizingMaskIntoConstraints = false
      

    【讨论】:

    • 不幸的是,如果我设置 insertview.translatemask=false 则插入视图不会显示。
    • 当然那你必须自己添加足够的约束。
    • 由 NSLayoutConstraint.constraintsWithVisualFormat("H:|-[...如何使用自动布局插入视图
    • 简单的回答:如果你什么都看不到,那么约束是不够的。尝试通过在 Interface Builder 中创建视图并在 UI 中设置约束来模拟您的环境。然后将其翻译成视觉格式语言。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-20
    • 2016-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多