如何让子视图使用视觉格式约束填充其超级视图?

时间:2015-09-07 07:02:40

标签: ios swift visual-format-language

这里是代码,我创建了一个视图并将其添加到self(继承自MKMapView):

private func setup() {

    coverView = UIView()
    coverView.backgroundColor = UIColor.greenColor()
    coverView.layer.zPosition = 10
    addSubview(coverView)
    addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(
        "H:|[cover]|",
        options: NSLayoutFormatOptions.allZeros,
        metrics: nil,
        views: ["cover": coverView]
        ) as! [NSLayoutConstraint])
    addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(
        "V:|[cover]|",
        options: NSLayoutFormatOptions.allZeros,
        metrics: nil,
        views: ["cover": coverView]
        ) as! [NSLayoutConstraint])
}

它说:

2015-09-07 14:53:59.234 ULine[48467:825531] 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) 
(
    "<NSLayoutConstraint:0x7fdd23320250 H:[UIView:0x7fdd2331f860]-(10)-|   (Names: '|':ULine.ULineMap:0x7fdd20e25bc0 )>",
    "<NSLayoutConstraint:0x7fdd232bb840 H:[ULine.ULineMap:0x7fdd20e25bc0]-(0)-|   (Names: '|':UIView:0x7fdd23320910 )>",
    "<NSLayoutConstraint:0x7fdd232bb890 H:|-(0)-[ULine.ULineMap:0x7fdd20e25bc0]   (Names: '|':UIView:0x7fdd23320910 )>",
    "<NSAutoresizingMaskLayoutConstraint:0x7fdd232c0280 h=--& v=--& UIView:0x7fdd2331f860.midX ==>",
    "<NSAutoresizingMaskLayoutConstraint:0x7fdd232c0af0 h=--& v=--& H:[UIView:0x7fdd2331f860(0)]>",
    "<NSLayoutConstraint:0x7fdd237ee370 'UIView-Encapsulated-Layout-Width' H:[UIView:0x7fdd23320910(375)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7fdd23320250 H:[UIView:0x7fdd2331f860]-(10)-|   (Names: '|':ULine.ULineMap:0x7fdd20e25bc0 )>

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

很奇怪,我只想填写超级视图。 感谢

1 个答案:

答案 0 :(得分:0)

我发帖后解决了,因为coverView启用了&#34; TranslatesAutoresizingMaskIntoConstraints&#34;,我只是添加coverView.setTranslatesAutoresizingMaskIntoConstraints(false),不再添加额外的约束。

希望它对你有所帮助。