I set some NSLayoutConstrains
in storyboard. I change a subview's frame in viewDidload
, it doesn't work. I change it in viewWillAppear
, it doesn't work. I change it in viewDidAppear
, it works. So when I set up constrains in storyboard
,
when do that constrains work to set the subviews' frame? After it works, I can set the view frame .Thank you!
Here are the animations I want to make, I can animate the frame and constrains, it shows the same, the code is:
Frame Version:
in viewDidload
, i first set the init frame,
self.containerView.frame = CGRectMake(0, UIScreen.mainScreen().bounds.size.height, self.containerView.frame.size.width, self.containerView.frame.size.height)
then in viewWillAppear
UIView.animateWithDuration(0.5, animations: { () -> Void in
self.containerView.frame = CGRectMake(0, UIScreen.mainScreen().bounds.size.height - self.containerView.frame.size.height, self.containerView.frame.size.width, self.containerView.frame.size.height)
self.backgroundButton.backgroundColor = UIColor(red:0, green:0, blue:0, alpha:0.5)
}, completion: nil)
The result is the containerView come up from down to show up. After I search on internet, I should change the constrains constant rather than the frame. so I change to code to:
override func viewDidLoad() {
super.viewDidLoad()
self.bottomConstrain.constant = -self.containerView.frame.size.height
}
and int
UIView.animateWithDuration(0.5, animations: { () -> Void in
self.bottomConstrain.constant = 0
self.view.layoutIfNeeded()
self.backgroundButton.backgroundColor = UIColor(red:0, green:0, blue:0, alpha:0.5)
}, completion: nil)
All the two methods work the same. So i also wonder what the difference between them.