2015-08-19 8 views
12

ho scorrere vista come @IBOutletCome cambiare il layout vincolo fondo in iOS, Swift

@IBOutlet weak var mainScrollView: UIScrollView! 

voglio cambiare il vincolo

"Bottom space to: Bottom Layout Guide" 

di programmazione.

First Item : Bottom Layout Guide.Top 
Relation : Equal 
Second Item: Scroll View.Bottom 
Constant: 0 -> 50 // (I want to change this programmatically) 
Priority: 1000 
Multiplier: 1 

Come posso fare questo?

risposta

23

Prendere il vincolo come IBOutlet di NSLayoutConstraint.

enter image description here

Impostare i punti di vincolo e cambiare constant valore:

self.sampleConstraint.constant = 20 
self.view.layoutIfNeeded() 
+0

Dove è il posto migliore per cambiarlo? –

12

Se si aggiunge vincolo programatically così:

var constraintButton = NSLayoutConstraint (item: buttonPlay, 
              attribute: NSLayoutAttribute.Bottom, 
              relatedBy: NSLayoutRelation.Equal, 
              toItem: self.view, 
              attribute: NSLayoutAttribute.Bottom, 
              multiplier: 1, 
              constant: 0) 
// Add the constraint to the view 
self.view.addConstraint(constraintButton) 

poi si può aggiornare in questo modo:

self.constraintButton.constant = 50 
self.view.layoutIfNeeded() 

E se volete che con l'animazione si può fare in questo modo:

self.view.layoutIfNeeded() 
UIView.animateWithDuration(1, animations: { 
    self.constraintButton.constant = 50 
    self.view.layoutIfNeeded() 
}) 

Speranza che aiuta.

5

Crea un IBOutlet per il vostro vincolo:

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomContraint; 

E quando avete bisogno di cambiarlo chiamare:

bottomContstraint.constant = //your value 
view.layoutIfNeeded() 

Inoltre è possibile animare il cambiamento vincolo del genere:

bottomContstraint.constant = //your value 

UIView.animateWithDuration(0.5, animations: { 
    self.view.layoutIfNeeded() 
}) 
+0

grazie @njuri, ha funzionato per me. – elia

Problemi correlati