UI 从代码创建的不会显示
UI created from code won't show up
我正在尝试从代码创建用户界面。这是代码:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let superview = self.view
let myLabel = UILabel()
myLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
myLabel.text = "My Label"
let myButton = UIButton()
myButton.setTitle("My Button", forState: UIControlState.Normal)
myButton.backgroundColor = UIColor.blueColor()
myButton.setTranslatesAutoresizingMaskIntoConstraints(false)
superview.addSubview(myLabel)
superview.addSubview(myButton)
var myConstraint=NSLayoutConstraint(item: myLabel, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: superview, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0)
superview.addConstraint(myConstraint)
myConstraint=NSLayoutConstraint(item: myLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: myButton, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0)
superview.addConstraint(myConstraint)
myConstraint=NSLayoutConstraint(item: myButton, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: myLabel, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: -10)
superview.addConstraint(myConstraint)
myConstraint=NSLayoutConstraint(item: myButton, attribute: NSLayoutAttribute.Baseline, relatedBy: NSLayoutRelation.Equal, toItem: myLabel, attribute: NSLayoutAttribute.Baseline, multiplier: 1.0, constant: 0)
superview.addConstraint(myConstraint)
}
根据我正在阅读的书籍教程,这段代码应该创建一个按钮和一个标签。当我 运行 模拟器没有任何显示(空白模拟器)。你能帮我找出错误吗?
你的第三个约束
myConstraint=NSLayoutConstraint(item: myButton, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: myLabel, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: -10)
尝试并排放置标签和按钮。
你的第二个约束
myConstraint=NSLayoutConstraint(item: myLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: myButton, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0)
尝试将它们一个放在另一个上面。
这两个约束之一将不得不取消!
我正在尝试从代码创建用户界面。这是代码:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let superview = self.view
let myLabel = UILabel()
myLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
myLabel.text = "My Label"
let myButton = UIButton()
myButton.setTitle("My Button", forState: UIControlState.Normal)
myButton.backgroundColor = UIColor.blueColor()
myButton.setTranslatesAutoresizingMaskIntoConstraints(false)
superview.addSubview(myLabel)
superview.addSubview(myButton)
var myConstraint=NSLayoutConstraint(item: myLabel, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: superview, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0)
superview.addConstraint(myConstraint)
myConstraint=NSLayoutConstraint(item: myLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: myButton, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0)
superview.addConstraint(myConstraint)
myConstraint=NSLayoutConstraint(item: myButton, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: myLabel, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: -10)
superview.addConstraint(myConstraint)
myConstraint=NSLayoutConstraint(item: myButton, attribute: NSLayoutAttribute.Baseline, relatedBy: NSLayoutRelation.Equal, toItem: myLabel, attribute: NSLayoutAttribute.Baseline, multiplier: 1.0, constant: 0)
superview.addConstraint(myConstraint)
}
根据我正在阅读的书籍教程,这段代码应该创建一个按钮和一个标签。当我 运行 模拟器没有任何显示(空白模拟器)。你能帮我找出错误吗?
你的第三个约束
myConstraint=NSLayoutConstraint(item: myButton, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: myLabel, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: -10)
尝试并排放置标签和按钮。
你的第二个约束
myConstraint=NSLayoutConstraint(item: myLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: myButton, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0)
尝试将它们一个放在另一个上面。
这两个约束之一将不得不取消!