gpt4 book ai didi

ios - 在 Swift 中为 UIButton 设置自动布局约束

转载 作者:行者123 更新时间:2023-11-28 09:37:57 24 4
gpt4 key购买 nike

我仍在努力了解 XCode 6.3 中的 Swift Autolayouts。我已经设置了一个 View Controller ,我在 Interface Builder 中贴了几个标签。我已经以编程方式添加了一个 UITextView 和 UIImageView,并且正在尝试在屏幕底部添加一个 UIButton 并以编程方式设置其约束。但是,当 View 尝试加载时,我不断收到错误消息。

类定义下:

let infoButton = UIButton.buttonWithType(UIButtonType.System) as! UIButton
var descView: UITextView!
var picView: UIImageView!

override func viewDidLoad() {
super.viewDidLoad()
infoButton.setTranslatesAutoresizingMaskIntoConstraints(false)
infoButton.setTitle("Information",forState:UIControlState.Normal)
self.view.addSubview(infoButton)

self.descView = UITextView(frame:CGRectMake(0,0,300,290))
self.descView.selectable = false
self.userInteractionEnabled = false
self.descView.text = "testing block of text"

self.container.frame = CGRect(x:20,y:160,width:300,height:290)
self.picView = UIImageView(image:"house.png")
self.view.addSubview(container)
self.container.addSubview(picView)

//setup button constraints
let viewsDictionary = ["infoButton":infoButton]
let button_constraint_H:Array = NSLayoutConstraint.constraintsWithVisualFormat("H:|-[infoButton]-|", options:NSLayoutFormatOptions(0),metrics:nil,views:viewsDictionary)
let button_constraint_V:Array = NSLayoutConstraint.constraintsWithVisualFormat("V:[infoButton]-0-|",options:NSLayoutFormatOptions(0),metrics:nil, views:viewsDictionary)
infoButton.addConstraints(button_constraint_H)
infoButton.addConstraints(button_constraint_V)

}

应用程序在 viewDidLoad() 方法中崩溃。

2015-06-17 16:59:14.902 LayoutTest[20323:5937743] The view hierarchy is not prepared for the constraint: <NSLayoutConstraint:0x7fc5b9de4140 UIButton:0x7fc5b9ec3e40'Information'.leading == UIView:0x7fc5b9eaf860.leadingMargin>
When added to a view, the constraint's items must be descendants of that view (or the view itself). This will crash if the constraint needs to be resolved before the view hierarchy is assembled. Break on -[UIView _viewHierarchyUnpreparedForConstraint:] to debug.
2015-06-17 16:59:14.904 LayoutTest[20323:5937743] View hierarchy unprepared for constraint.
Constraint: <NSLayoutConstraint:0x7fc5b9de4140 UIButton:0x7fc5b9ec3e40'Information'.leading == UIView:0x7fc5b9eaf860.leadingMargin>
Container hierarchy:
<UIButton: 0x7fc5b9ec3e40; frame = (0 0; 0 0); opaque = NO; layer = <CALayer: 0x7fc5b9ee2a80>>
View not found in container hierarchy: <UIView: 0x7fc5b9eaf860; frame = (0 0; 375 667); autoresize = W+H; layer = <CALayer: 0x7fc5b9ed9c70>>
That view's superview: NO SUPERVIEW
2015-06-17 16:59:14.925 LayoutTest[20323:5937743] *** Terminating app due to uncaught exception 'NSGenericException', reason: 'Unable to install constraint on view. Does the constraint reference something from outside the subtree of the view? That's illegal. constraint:<NSLayoutConstraint:0x7fc5b9de4140 UIButton:0x7fc5b9ec3e40'Information'.leading == UIView:0x7fc5b9eaf860.leadingMargin> view:<UIButton: 0x7fc5b9ec3e40; frame = (0 0; 0 0); opaque = NO; layer = <CALayer: 0x7fc5b9ee2a80>>'

我究竟做错了什么?
我已经使用 Interface Builder 为其他项目(标签)定义了一些约束,但需要能够定义按钮等,并通过代码为同一 View 设置约束。

这是我与 Swift 相处的第一周,所以请保持温柔!进

最佳答案

试试这段代码

        self.view.addConstraint(NSLayoutConstraint(item: infoButton, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 0.0))
self.view.addConstraint(NSLayoutConstraint(item: infoButton, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Trailing, multiplier: 1.0, constant: 0.0))
self.view.addConstraint(NSLayoutConstraint(item: infoButton, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0.0))

关于ios - 在 Swift 中为 UIButton 设置自动布局约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30897112/

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