2016-05-17 3 views
-1

Так что у меня был перерыв с iOS dev на 4 месяца, и, похоже, я все забыл. Все, что я пытаюсь сделать, это поместить Label программно на 0,0, размер 200,50. Я слышал, что в iOS8 есть несколько изменений, которые я не помню.Swift: добавление ограничений в UILabel Programmatically

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]) 

ответ

0

Похоже, я был свалкой!

Это было событие, от которого я звонил, а не код. Перемещено в viewWillAppear

Смежные вопросы