0

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.

Nikunj
  • 655
  • 3
  • 13
  • 25
leizh00701
  • 1,893
  • 5
  • 21
  • 32
  • If subview is constrained, never try changing its `frame` manually. – Ozgur Vatansever Nov 10 '15 at 05:13
  • I know, I should change the constrain constant, but I change the frame, it alse works. The reason I want to change the frame is that I want to make animation combined the view backgroundcolor, I can do that in UIView.animaton(_,duration:,completion).But if I change the constrain seperately, I donnot know how to do that animation – leizh00701 Nov 10 '15 at 05:17

2 Answers2

1

So when I set up constrains in storyboard, when do that constrains work to set the subviews' frame?

Between viewWillLayoutSubviews and viewDidLayoutSubviews.

matt
  • 515,959
  • 87
  • 875
  • 1,141
0

Please try below code. Hope it is work for you.

- (void)viewWillLayoutSubviews
{
      //set your constant outside the animation 
      self.YOUR_SUB_VIEW_CONSTRAINT.constant = 0; //As per your requirement whatever constant value you can set. 

      [UIView animateWithDuration:0.2 animations:^{
          [self.view layoutIfNeeded];
      }];
}

It's change your constraint with animation.

Thanks :)

Rupal Patel
  • 570
  • 6
  • 13