57
votes

I have a custom UIView which has a dedicated, manually set frame for portrait and landscape orientation because autoresizingMasks just don't work in my case.

I set this frame in:

 - (void)viewWillAppear:(BOOL)animated

And it works as expected.

My problem is now, that my UIView furthermore has subviews that also have a dedicated position for portrait/landscape.

I position these subviews in:

- (void)layoutSubviews

If I now leave my viewController in e.g. portrait orientation, rotate to landscape on another viewController and then come back to this viewController I see that my view got resized correctly, but the subviews of my view, which get positioned in layoutSubviews are not repositioned yet but are resized in an animated fashion after the view already appeared.

I do nothing in viewDidAppear and I already tried calling

 - (void)layoutIfNeeded

As well as:

 - (void)setNeedsLayout

In viewWillAppear, but it doesn't seem to change anything.

Any ideas what I'm doing wrong?

4

4 Answers

107
votes

Had a similar problem today. I solved it by first calling

  • -setNeedsLayout: Set the flag that the view needs layout, and afterwards calling
  • -layoutIfNeeded: Check the flag immediately and - as it was set manually before - as result layoutSubviews is called.

Don't know if this is the best way, but it works ;)

5
votes

I would suggest setting the initial frame in viewDidLoad then changing the view frame for orientation changes in

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration

You don't need any explicit animations in layoutSubviews (not sure if you had any, but it sounded like it) as any frame changes will be synced to the rotation animation.

When off screen the orientation should still be rotated, so your views should be positioned correctly when transitioned back on screen again.

I also recommend watching WWDC 2010 Session 123 "Building Animation Driven UI" which covers this stuff in much more detail.

4
votes

At swift 3 @iOS 10.1

override func viewWillAppear(animated: Bool){

    super.viewWillAppear(animated)
    view.setNeedsLayout()
}

runs perfect

-2
votes

For me what worked is [self.view setNeedsDisplay] . This calls redraw on the whole view. You can also redraw a selected view by calling setNeedsDisplay on the particular view, which I did in my case and it worked perfectly.