所以我试图使用以下代码以编程方式为我的视图添加约束。我正在尝试添加一些32的顶部空间和一些16的前导空间到一个视图,它以编程方式添加到我的一个视图控制器,但我似乎遇到了一些问题。
下面是我如何将按钮返回到我的视图的示例,以便以编程方式添加该按钮。
// 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属性必须是视图的错误。但是现在当我使用这些属性运行代码时,我收到以下错误。
'NSInternalInconsistencyException',原因:'无法设置视图层次结构的布局,无法用于约束。'
我正在尝试将此按钮添加到一个视图中,该视图也是以编程方式创建的,如下所示。
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
}
答案 0 :(得分:3)
一些想法:
以编程方式创建视图时,translatesAutoresizingMaskIntoConstraints
默认为true
。关掉它。
在将约束添加到视图层次结构之前,您是将约束设置到另一个视图(其超级视图?)。因此,请确保(a)首先执行addSubiew
并且(b)应将此约束添加到最近的公共父级(通常是超级视图),而不是按钮本身。可以将.NotAnAttribute
约束添加到按钮,但其余约束应添加到最近的公共父项。
您正在设置约束和frame
。如果你做前者,则不需要后者。
如果您在解决上述问题后仍然无法正常工作,请分享错误的全文,而不仅仅是单行消息。