2016-06-24 19 views
2

Projekt Github repository.ändern Einschränkungen mit NSLayoutConstraint

So zum Beispiel, ich das ändern:

let orangeViewCenterXConstraint = NSLayoutConstraint(
    item: orangeView, 
    attribute: .centerX, 
    relatedBy: .equal, 
    toItem: view, 
    attribute: .centerX, 
    multiplier: 1.0, 
    constant: 0.0 
) 

Dazu:

orangeView.centerXAnchor.constraint(equalTo: view.centerXAnchor) 

Wie ist der richtige Weg, das gleiche zu tun mit:

let purpleViewBottomSpaceConstraint = NSLayoutConstraint(
    item: purpleView, 
    attribute: .bottom, 
    relatedBy: .equal, 
    toItem: orangeView, 
    attribute: .top, 
    multiplier: 1.0, 
    constant: -8.0 
) 

Ich habe versucht, mit :

purpleView.bottomAnchor.constraint(equalTo: <#T##NSLayoutAnchor<AnyObject>#>, constant: <#T##CGFloat#>) 

Aber ich denke purpleView braucht einen Platz zwischen purpleView und orangeView, also technisch , constant: -8.0, aber das ist falsch.

Ich bin mir nicht sicher, was ich gerade mache, also bin ich hier um zu lernen.

+0

Problem gelöst !, überprüfen Sie die Github, wenn Sie irgendeine Frage haben;) –

Antwort

1

Sie haben die topAnchor angeben, zum Beispiel:

NSLayoutConstraint.activate([ 
    purpleView.bottomAnchor.constraint(equalTo: orangeView.topAnchor, constant: -8) 
]) 
+0

Dank !, in xcode8 ist: 'purpleView.bottomAnchor.constraint (equalTo: orangeView.topAnchor, Konstante: -8) ' –

+0

Ja, in Xcode 8 ist das die neue Syntax. Aktualisierte Antwort entsprechend. – Rob

+0

Ich habe eine andere Frage: 'lassen purpleViewTopSpaceConstraint = NSLayoutConstraint (item: purpleView, Attribut: .top, relatedBy: .equal, toItem: self.topLayoutGuide, Attribut: .bottom, Multiplikator: 1,0, konstant : 8.0) ' –

Verwandte Themen