ios - 自动布局将尝试通过打破约束来恢复

标签 ios autolayout nslayoutconstraint visual-format-language

我想在其superView的右上角放置一个标签,并在其底部添加8pt尾部和10pt
这是我的代码:

  let label = UILabel(frame: CGRectZero)
  label.text = "text goes here"
  label.textColor = UIColor.whiteColor()
  label.translatesAutoresizingMaskIntoConstraints = false
  label.numberOfLines = 0
  label.textAlignment = .Right
  label.backgroundColor = UIColor.blackColor()
  label.font = UIFont(name: "TheSans-Plain", size: 17)
  imageTitleContainer.addSubview(label)


    let label_constraint_H:Array = NSLayoutConstraint.constraintsWithVisualFormat("H:|-(>=8)-[label]-(8)-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["label":label])

    let label_constraint_V:Array = NSLayoutConstraint.constraintsWithVisualFormat("V:[label]-(10)-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["label":label])

    imageTitleContainer.addConstraints(label_constraint_H)
    imageTitleContainer.addConstraints(label_constraint_V)`

但我在Unable to simultaneously satisfy constraints中得到consoleWill attempt to recover by breaking constraint <NSLayoutConstraint:0x7ff55acf24c0 H:[UILabel:0x7ff55acf1af0'text goes here']-(8)-| (Names: '|':UIView:0x7ff55acbf430 )>
注意:0x7ff55acbf430是我在代码中的imageTitleContainer
更新:其他限制:"<NSLayoutConstraint:0x7ff55accdc80 H:|-(0)-[UIView:0x7ff55acbf430] (Names: '|':UIView:0x7ff55acca080 )>", "<NSLayoutConstraint:0x7ff55accdcd0 H:[UIView:0x7ff55acbf430]-(0)-| (Names: '|':UIView:0x7ff55acca080 )>", "<NSLayoutConstraint:0x7ff55acf2450 H:|-(>=8)-[UILabel:0x7ff55acf1af0'text goes here'] (Names: '|':UIView:0x7ff55acbf430 )>", "<NSLayoutConstraint:0x7ff55acf24c0 H:[UILabel:0x7ff55acf1af0'text goes here']-(8)-| (Names: '|':UIView:0x7ff55acbf430 )>", "<NSLayoutConstraint:0x7ff55adb3830 'UIView-Encapsulated-Layout-Width' H:[UIView:0x7ff55acca080(0)]>" )

最佳答案

尝试降低您的约束之一的优先级,例如

H:|-(>=8@999)-[label]-(8)-|

必须这样做以允许UICollectionView进行其布局工作。请注意名为UIView-Encapsulated-Layout-Width的零宽度约束,它会破坏您的布局。

关于ios - 自动布局将尝试通过打破约束来恢复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34390179/

相关文章:

IOS-在应用程序中使用设置包?

ios - 加载通用类型在模拟器中有效,但在设备中无效

ios - 如果使用自动布局,何时检查呈现的 viewController 是否属于类型

swift - 使用 SnapKit 自动调整 UILabel 的大小

iOS 自动布局 : Issues with 2 variable sized boxes and 1 fixed sized box

iOS:以编程方式更改从 Interface Builder 设置的约束

ios - 如果我只是在其中添加 ScrollView ,NSLayoutConstraint 会给出很多警告

ios - HeyZap (SDK 10.2.1) 未调用 IOS 委托(delegate)函数

ios - 在底部滚动不起作用

ios - 堆栈 View 中堆栈 View 的约束