ios - 如何使用视觉格式约束让 subview 填充其 super View ?

标签 ios swift visual-format-language

这是代码,我创建了一个 View 并将其添加到 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.

这很奇怪,我只是想让它填满 super View 。 谢谢

最佳答案

我发帖后解决了这个问题,因为 coverView 启用了“TranslatesAutoresizingMaskIntoConstraints”,我只是简单地添加 coverView.setTranslatesAutoresizingMaskIntoConstraints(false),不再添加额外的约束。

希望对您有帮助。

关于ios - 如何使用视觉格式约束让 subview 填充其 super View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32432882/

相关文章:

iphone - 为什么我的应用程序中着色器的路径为零?

android - 如何使用钛在android中应用幻灯片动画?

ios - 重新加载后 swift 保持 TableView 数据位置

ios - 将可视格式的约束转换为 NSLayoutAnchor API

ios - 使用可视格式语言将 View 置于其父 View 的中心

ios - 集成到 ios 应用程序时出现 Pnet 模型问题

iOS Swift - 有没有办法将 subview 添加到表格单元格中并正确定位它?

ios - 将背景图像保持在动画的中心

ios - NSLayoutRelationConstraint 到视觉格式约束

ios - Swift Xcode 无法从项目中删除嵌入式二进制文件/链接框架和库