NSLayout 约束失败

NSLayout Constraints failing

所以我正在尝试使用以下代码以编程方式向我的视图添加约束。我正在尝试将一些 top space of 32 和一些 leading space of 16 添加到以编程方式添加到我的视图控制器之一的视图中,但我似乎是 运行ning成一些问题。

下面是我如何将按钮返回到我的视图以便以编程方式添加它的示例。

// Creating the button
func createDismissButton(vwParentView: OnboardingViewController) -> UIButton {

     let dismissButton = UIButton()
     dismissButton.setImage(UIImage(named: "Close"), forState: .Normal)
     dismissButton.frame =  CGRectMake(16, 32, 34, 34)
     dismissButton.addTarget(self, action: "dismiss:", forControlEvents: .TouchUpInside)

     // AUTO LAYOUT

    // WIDTH & HEIGHT
    let dismissWidthButtonConstraint = NSLayoutConstraint (item: dismissButton,
        attribute: NSLayoutAttribute.Width,
        relatedBy: NSLayoutRelation.Equal,
        toItem: nil,
        attribute: NSLayoutAttribute.NotAnAttribute,
        multiplier: 1,
        constant: 34)

    let dismissHeightButtonConstraint = NSLayoutConstraint (item: dismissButton,
        attribute: NSLayoutAttribute.Width,
        relatedBy: NSLayoutRelation.Equal,
        toItem: nil,
        attribute: NSLayoutAttribute.NotAnAttribute,
        multiplier: 1,
        constant: 34)

    // SPACING
    let dismissButtonTopConstraint = NSLayoutConstraint(item: dismissButton,
        attribute: .Top,
        relatedBy: .Equal,
        toItem: vwParentView.view,
        attribute: .Top,
        multiplier: 1.0,
        constant: 32)

    let dismissButtonLeftConstraint = NSLayoutConstraint(item: dismissButton,
        attribute: .Leading,
        relatedBy: .Equal,
        toItem: vwParentView.view,
        attribute: .Leading,
        multiplier: 1.0,
        constant: 16)


    dismissButton.addConstraint(dismissWidthButtonConstraint)
    dismissButton.addConstraint(dismissHeightButtonConstraint)

    dismissButton.addConstraint(dismissButtonTopConstraint)
    dismissButton.addConstraint(dismissButtonLeftConstraint)

     return dismissButton
}

但是我添加 vwParentView.view 的间距似乎失败了,因为它在日志中显示了一个关于项目 属性 必须是视图的错误。但是现在当我 运行 具有这些属性的代码时,我收到以下错误。

'NSInternalInconsistencyException',原因:'Impossible to set up layout with view hierarchy unprepared for constraint.'

我正在尝试将此按钮添加到视图中,该视图也是通过编程方式创建的,如下所示。

override func viewWillAppear(animated: Bool) {
    // Add the view below to the current view controller
    self.view.addSubview(generatePurchasePaging().view)
}

func generatePurchasePaging() -> OnboardingViewController {

    let welcomePage = OnboardingContentViewController(title: "PAY WHAT YOU WANT", body: "I made my app so you could have the best experience reading tech related news. That’s why I want you to value it based on your satisfaction.", image: UIImage(named: "Purchase-Pig"), buttonText: "") { () -> Void in

    }

    let firstPurchasePage = OnboardingContentViewController(title: "MINT", body: "The app is great but there’s still a few places in room of improvement. If this is your feeling this is for you.", image: UIImage(named: "Purchase-Mint"), buttonText: "69p") { () -> Void in

    }

    let secondPurchasePage = OnboardingContentViewController(title: "SWEET", body: "IThis is the suggested price where you value the time I spent on development and design. Feel free to pay more or less.", image: UIImage(named: "Purchase-Lolly"), buttonText: "£1.49") { () -> Void in

    }

    let thirdPurchasePage = OnboardingContentViewController(title: "GOLD", body: "Hello is it me your looking for, if this popped into your mind using the app then this is the price for you.", image: UIImage(named: "Purchase-Coin"), buttonText: "£2.99") { () -> Void in

    }

    let purchaseVC = OnboardingViewController(backgroundImage: nil, contents: [welcomePage, firstPurchasePage, secondPurchasePage, thirdPurchasePage])
   purchaseVC.shouldMaskBackground = false

    purchaseVC.view.addSubview(createDismissButton(purchaseVC))
    return purchaseVC
}

一些想法:

  1. 当您以编程方式创建视图时,translatesAutoresizingMaskIntoConstraints 默认为 true。关闭此功能。

  2. 您在将另一个视图(它的父视图?)添加到视图层次结构之前设置约束。所以确保(a)你首先执行 addSubiew 和(b)这个约束应该被添加到最近的公共父视图(通常是父视图),而不是按钮本身。 .NotAnAttribute 约束可以添加到按钮,但其余的应该添加到最近的公共父级。

  3. 您正在设置约束和 frame。如果你做前者,后者就不需要了。

如果您在采取上述措施后仍然无法正常工作,请分享错误的全文,而不仅仅是单行消息。