2017-07-11 39 views
0

我有一个视图控制器中的容器视图,其中包含一个按钮,每次按下时都会在父视图控制器中向下移动标签。我将标签的约束传递给容器,通过准备segue。从那里我可以修改约束,但它不会动画。如何在容器视图中为父视图控制器中的约束更改设置动画? (Swift 4)

我应该怎么做?

visual example

class ViewController: UIViewController { 

    @IBOutlet weak var LabelTopConstraint: NSLayoutConstraint! 

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
     if segue.identifier == "containerSegue" { 
      let container = segue.destination as? Container 
      container?.LabelTopConstraint = LabelTopConstraint 
     } 
    } 
} 

class Container: UIViewController { 

    weak var LabelTopConstraint: NSLayoutConstraint! 

    @IBAction func MoveLabelButtonPressed(_ sender: Any) { 
     LabelTopConstraint.constant += 30 
     UIView.animate(withDuration: 0.35, animations: { 
      self.view.layoutIfNeeded() 
     }) 
    } 
} 

回答

1
您需要更新父视图一样的布局

100%的工作答案..

@IBAction func MoveLabelButtonPressed(_ sender: Any) { 


    self.LabelTopConstraint.constant += 30 // Your constraint constant change 


    UIView.animate(withDuration: 3.0) { 
     self.parent?.view.layoutIfNeeded() // Force lays of all subviews on parent view . 
    } 

} 

OUTPUT:

enter image description here

相关问题