Swift:以编程方式向UILabel添加约束

Swift:以编程方式向UILabel添加约束,swift,autolayout,uilabel,ios-autolayout,Swift,Autolayout,Uilabel,Ios Autolayout,因此,我从iOS开发中断了4个月,似乎我已经忘记了一切。我所要做的就是以编程方式将标签放置为0,0,大小为200,50。我听说iOS8有一些变化,我不记得了 let x : CGFloat = 0.0 let y : CGFloat = 0.0 let width : CGFloat = 200.0 let height : CGFloat = 50.0 self.label = UILabel(frame: CGRect(x: x, y: y, width: width, height: he

因此,我从iOS开发中断了4个月,似乎我已经忘记了一切。我所要做的就是以编程方式将标签放置为0,0,大小为200,50。我听说iOS8有一些变化,我不记得了

let x : CGFloat = 0.0
let y : CGFloat = 0.0
let width : CGFloat = 200.0
let height : CGFloat = 50.0
self.label = UILabel(frame: CGRect(x: x, y: y, width: width, height: height))
self.label.text = "SIMON"
self.label.textColor = UIColor.whiteColor()

self.label.font = UIFont(name: "HelveticaNeue-UltraLight", size: 24)
self.label.textAlignment = NSTextAlignment.Center
self.label.backgroundColor = UIColor.redColor()
self.label.layer.masksToBounds = true;
self.label.layer.cornerRadius = 8.0;
self.label.adjustsFontSizeToFitWidth = true;

self.label.translatesAutoresizingMaskIntoConstraints = false

widthConstraint = NSLayoutConstraint(item: self.label, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: width)

heightConstraint = NSLayoutConstraint(item: self.label, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: height)

leftConstraint = NSLayoutConstraint(item: self.label, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.LeadingMargin, multiplier: 1, constant: x)

topConstraint = NSLayoutConstraint(item: self.label, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.TopMargin, multiplier: 1, constant: y)

self.view.addSubview(self.label)

NSLayoutConstraint.activateConstraints([leftConstraint,topConstraint, widthConstraint, heightConstraint] )

看来我被甩了

这是我调用它的事件,而不是代码。移动到视图将显示