gpt4 book ai didi

swift - NSLayout 约束失败

转载 作者:可可西里 更新时间:2023-11-01 01:02:22 24 4
gpt4 key购买 nike

所以我尝试使用以下代码以编程方式向我的 View 添加约束。我正在尝试将 32 的顶部空间和 16 的前导空间添加到以编程方式添加到我的 View Controller 之一的 View 中,但我似乎遇到了一些问题。

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

// 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 的间距上失败了,因为它在日志中出现了关于项目属性必须是 View 的错误。但是现在,当我使用这些属性运行代码时,出现以下错误。

“NSInternalInconsistencyException”,原因:“无法使用未准备好约束的 View 层次结构来设置布局。”

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

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. 当您以编程方式创建 View 时,translatesAutoresizingMaskIntoConstraints默认为 true。关闭此功能。

  2. 在将另一个 View (它的父 View ?)添加到 View 层次结构之前,您要对其设置约束。因此,请确保 (a) 您首先执行 addSubiew 并且 (b) 应将此约束添加到最近的公共(public)父级(通常是父 View ),而不是按钮本身。 .NotAnAttribute 约束可以添加到按钮,但其余的应该添加到最近的公共(public)父级。

  3. 您同时设置了约束和框架。如果你做前者,则不需要后者。

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

关于swift - NSLayout 约束失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34285045/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com