How to change bottom layout constraint in iOS, Swift

前端 未结 3 2090
深忆病人
深忆病人 2020-12-25 14:39

I have scroll view as @IBOutlet

@IBOutlet weak var mainScrollView: UIScrollView!

I want to change the

\"Bottom space to: B         


        
相关标签:
3条回答
  • 2020-12-25 15:17

    Take the constraint as IBOutlet of NSLayoutConstraint.

    enter image description here

    Set the constraint outlets and change constant value by :

    self.sampleConstraint.constant = 20
    self.view.layoutIfNeeded()
    
    0 讨论(0)
  • 2020-12-25 15:18

    If you are adding constraint programatically like this:

    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)
    

    Then you can update it this way:

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

    And if you want that with animation you can do it this way:

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

    Hope it helps.

    0 讨论(0)
  • 2020-12-25 15:20

    Create an IBOutlet for your constraint:

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

    And when you need to change it call:

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

    Also you can animate constraint change like that:

    bottomContstraint.constant = //your value
    
    UIView.animateWithDuration(0.5, animations: {
      self.view.layoutIfNeeded()
    })
    
    0 讨论(0)
提交回复
热议问题