NSLayout约束失败 [英] NSLayout Constraints failing
问题描述
因此,我尝试使用以下代码以编程方式向我的视图添加约束.我正在尝试将32个顶部空间和16个前导空间添加到以编程方式添加到我的一个视图控制器中的视图中,但是我似乎遇到了一些问题.
So i'm trying to programatically add constraints to my view using the following code. I'm trying to add some top space of 32 and some leading space of 16 to a view which is being added programatically to one of my view controllers but i seems to be running into some issues.
下面是我如何将按钮返回到视图以便可以以编程方式添加按钮的示例.
Below is the example of how i'm returning the button to my view so it can be added programatically.
// 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
的间隔似乎失败了,因为它在日志中出现了关于item属性必须为视图的错误.但是现在,当我使用这些属性运行代码时,出现以下错误.
But it seems to be failing on the spacing i add vwParentView.view
since it was bring up an error in the log about the item property having to be a view. But now when i run the code with the these properties i'm getting the following error.
'NSInternalInconsistencyException',原因:无法设置视图层次结构未准备好约束的布局."
'NSInternalInconsistencyException', reason: 'Impossible to set up layout with view hierarchy unprepared for constraint.'
我正在尝试将此按钮添加到也已通过编程方式创建的视图中,如下所示.
I'm trying to add this button to a view which has also been created programatically like so below.
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
}
推荐答案
一些想法:
-
You are setting constraints to another view (its superview?) before adding it to the view hierarchy. So make sure (a) you first perform
addSubiew
and (b) this constraint should be added to the nearest common parent (usually the superview), not to the button, itself. The.NotAnAttribute
constraints can be added to the button, but the rest should be added to the nearest common parent.您正在设置约束和
frame
.如果您选择前者,则不需要后者.You are setting both the constraints and the
frame
. If you do the former, the latter is not needed.如果在解决上述问题后仍无法正常工作,请共享错误的全文,而不仅仅是一行信息.
If it's still not working after you remedy the above, please share the full text of the error, not just the one-line message.
这篇关于NSLayout约束失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!