【问题标题】:NSAutoresizingMaskLayoutConstraint errors in an AutoLayout-based application基于 AutoLayout 的应用程序中的 NSAutoresizingMaskLayoutConstraint 错误
【发布时间】:2014-08-20 20:36:23
【问题描述】:

由于与 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 )>

我不知道这些NSAutoresizingMaskLayoutConstraints 来自哪里,但据我了解,它们正在阻止secondaryView 移动。我做错了什么?

【问题讨论】:

    标签: ios swift autoresizingmask autolayout


    【解决方案1】:

    为 Swift 2 更新

    通过假设 secondaryView 不存在于 Storyboard 中并且只是一个没有任何 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))
        }
    }
    

    【讨论】:

    • 是的,你完全正确。 @IBOutlet 是项目的遗留物,这个问题最初发生在我试图创建一个最小的非工作示例时。而且我一定遇到过问题引起的隧道视觉问题,禁用 subview 上的自动调整蒙版翻译是正确的做法。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-11
    • 2020-09-08
    • 2017-02-18
    • 1970-01-01
    • 2019-05-16
    • 1970-01-01
    相关资源
    最近更新 更多