基于AutoLayout的应用程序中的NSAutoresizingMaskLayoutConstraint错误

时间:2014-08-20 20:36:24

标签: ios swift autolayout autoresizingmask nsautolayout

由于与AutoLayout相关的大量问题,我并不完全确定我会问一个新问题,但我无法让它发挥作用。我试图使用AutoLayout为视图设置动画,但我只是在翻译时失败了。在使用故事板之前我曾与AL合作,这是我第一次尝试以编程方式使用它们。

所以,问题是:从新创建的主 - 细节iOS应用程序开始,我从故事板中删除了所有内容,插入了一个新的视图控制器并将其视图的类设置为BaseView。基本视图的代码如下:

class BaseView: UIView {
  @IBOutlet var secondaryView: UIView! = nil

  override func awakeFromNib() {
    setTranslatesAutoresizingMaskIntoConstraints(false)
    backgroundColor = UIColor.redColor()
    secondaryView = UIView(frame: frame)
    secondaryView.backgroundColor = UIColor.blueColor()
    addSubview(secondaryView)
    let views = ["secondaryView": secondaryView, "self": self]
    addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-(100)-[secondaryView]", options: NSLayoutFormatOptions.AlignAllLeading, metrics: nil, views: views))
    addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[secondaryView(==self)]", options: NSLayoutFormatOptions(0), metrics: nil, views: views))
    addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[secondaryView(==self)]", options: NSLayoutFormatOptions(0), metrics: nil, views: views))
  }
}

我还尝试通过故事板的用户定义的运行时属性将translatesAutoresizingMaskIntoConstraints设置为false,但无济于事。我期望的是在模拟器的左侧看到一个红色方块(基本视图),模拟器屏幕的其余部分用蓝色填充。相反,一切都是蓝色的,日志会给我以下错误:

2014-08-20 22:19:00.392 AutoLayoutAnimationTest[5585:473139] 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:0x7fa8c94b5f50 H:|-(100)-[UIView:0x7fa8c94b2540]   (Names: '|':AutoLayoutAnimationTest.BaseView:0x7fa8c94afa80 )>",
    "<NSAutoresizingMaskLayoutConstraint:0x7fa8c94bdfc0 h=--& v=--& UIView:0x7fa8c94b2540.midX == + 300>",
    "<NSAutoresizingMaskLayoutConstraint:0x7fa8c94be1d0 h=--& v=--& H:[UIView:0x7fa8c94b2540(600)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7fa8c94b5f50 H:|-(100)-[UIView:0x7fa8c94b2540]   (Names: '|':AutoLayoutAnimationTest.BaseView:0x7fa8c94afa80 )>

我不知道这些NSAutoresizingMaskLayoutConstraint来自何处,但据我了解,他们阻止secondaryView移动。我做错了什么?

1 个答案:

答案 0 :(得分:1)

更新了Swift 2

通过假设secondaryView已经存在于故事板中并且只是一个没有任何IBOutlet的属性,我能够使以下代码在没有任何调试器投诉的情况下工作:

class BaseView: UIView {
    //@IBOutlet var secondaryView: UIView! = nil
    var secondaryView: UIView!

    override func awakeFromNib() {
        backgroundColor = UIColor.redColor()

        secondaryView = UIView()
        secondaryView.translatesAutoresizingMaskIntoConstraints = false // Swift 2
        // secondaryView.setTranslatesAutoresizingMaskIntoConstraints(false) // Swift 1.2
        secondaryView.backgroundColor = UIColor.blueColor()
        addSubview(secondaryView)

        let views = ["secondaryView": secondaryView, "self": self]
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-(100)-[secondaryView]", options: NSLayoutFormatOptions.AlignAllLeading, metrics: nil, views: views))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[secondaryView(==self)]", options: NSLayoutFormatOptions(0), metrics: nil, views: views))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[secondaryView(==self)]", options: NSLayoutFormatOptions(0), metrics: nil, views: views))
    }
}

因此,我认为以下代码(简洁)等效于前面的代码:

class BaseView: UIView {
    var secondaryView: UIView!

    override func awakeFromNib() {
        backgroundColor = UIColor.redColor()

        secondaryView = UIView()
        secondaryView.translatesAutoresizingMaskIntoConstraints = false // Swift 2
        // secondaryView.setTranslatesAutoresizingMaskIntoConstraints(false) // Swift 1.2
        secondaryView.backgroundColor = UIColor.blueColor()
        addSubview(secondaryView)

        let views = ["secondaryView": secondaryView]
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-(100)-[secondaryView]|", options: NSLayoutFormatOptions(0), metrics: nil, views: views))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[secondaryView]|", options: NSLayoutFormatOptions(0), metrics: nil, views: views))
    }
}