2016-08-03 60 views
0

Also ich möchte horizontal und vertikal ein UILabel in meiner Ansicht programmgesteuert zentrieren. Ich folge this topic, aber es funktioniert nicht. Wie kann ich das machen?Auto-Layout programmgesteuert mit UILabel funktioniert nicht

p/s: dies ist mein Code:

class ViewController: UIViewController { 
    override func viewDidLoad() { 
     super.viewDidLoad() 
     let noDataLabel = UILabel() 
     noDataLabel.text = "No data :(" 
     noDataLabel.textColor = UIColor.redColor() 
     noDataLabel.font = UIFont.systemFontOfSize(20) 
     noDataLabel.sizeToFit() 
     self.view.addSubview(noDataLabel) 
     NSLayoutConstraint(item: noDataLabel, attribute: .CenterX, relatedBy: .Equal, toItem: self.view, attribute: .CenterX, multiplier: 1.0, constant: 0.0).active = true 
     NSLayoutConstraint(item: noDataLabel, attribute: .CenterY, relatedBy: .Equal, toItem: self.view, attribute: .CenterY, multiplier: 1.0, constant: 0.0).active = true 
    } 
} 

Antwort

4

Fügen Sie dies Ihrem Code hinzu:

noDataLabel.translatesAutoresizingMaskIntoConstraints = false 

Von Apfel Dokumentation:

standardmäßig gibt die automatische Größenmaske auf einem Blick Anlass zu Einschränkungen, die nach Ansicht der Position vollständig bestimmen. Dadurch kann das automatische Layoutsystem die Rahmen von Ansichten verfolgen, deren Layout manuell gesteuert wird (z. B. über -setFrame:). Wenn Sie sich dafür entscheiden, die Ansicht mithilfe des automatischen Layouts zu positionieren, indem Sie eigene Einschränkungen hinzufügen, müssen Sie diese Eigenschaft auf setzen. IB wird das für dich tun.

+0

Vielen Dank! Es funktioniert jetzt!!! –

1

Sie müssen auch Größenbeschränkungen für uilabel wie unten und fügen Sie sie superview

NSLayoutConstraint(item: noDataLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100) 
NSLayoutConstraint(item: noDataLabel, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 40) 

Ihre Implementierung wie

aussehen schaffen
class ViewController: UIViewController { 
    override func viewDidLoad() { 
     super.viewDidLoad() 
     let noDataLabel = UILabel() 
     noDataLabel.text = "No data :(" 
     noDataLabel.textColor = UIColor.redColor() 
     noDataLabel.font = UIFont.systemFontOfSize(20) 
     noDataLabel.sizeToFit() 

     noDataLabel.translatesAutoresizingMaskIntoConstraints = false 

     self.view.addSubview(noDataLabel) 

     let horizontal = NSLayoutConstraint(item: noDataLabel, attribute: .CenterX, relatedBy: .Equal, toItem: self.view, attribute: .CenterX, multiplier: 1.0, constant: 0.0).active = true 
     let vertical = NSLayoutConstraint(item: noDataLabel, attribute: .CenterY, relatedBy: .Equal, toItem: self.view, attribute: .CenterY, multiplier: 1.0, constant: 0.0).active = true 
     let width = NSLayoutConstraint(item: noDataLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100) 
     let height = NSLayoutConstraint(item: noDataLabel, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 40) 

     self.view.addConstraint(horizontal) 
     self.view.addConstraint(vertical) 
     self.view.addConstraint(width) 
     self.view.addConstraint(height) 
    } 
} 
+0

Es funktioniert immer noch nicht :( –

+0

Als @Prajeet Shrestha antwortete add 'noDataLabel.translatesAutoresizingMaskIntoConstraints = false' Zeilen, aktualisierte ich die Antwort – iSashok

+0

Danke! Es funktioniert jetzt!!! –

0

noDataLabel.translatesAutoresizingMaskIntoConstraints = false noDataLabel.superview.addConstraint (NSLayoutConstraint (item: noDataLabel, attribute: .CenterX, relatedBy: .Equal, toItem: self.view, attribute: .CenterX, Dimension: 1,0, konstant : 0.0) .active = true) noDataLabel.superview.addConstraint (NSLayoutConstraint (Element: noDataLabel, Attribut: .CenterY, relatedBy: .Equal, toItem: self.view, Attribut: .CenterY, Multiplikator: 1.0, Konstante: 0.0) .active = tru)