How to force layoutSubviews of UIView?

IosIphoneUiviewUiviewcontrollerLayoutsubviews

Ios Problem Overview


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?

Ios Solutions


Solution 1 - Ios

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 ;)

Solution 2 - Ios

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.

Solution 3 - Ios

At swift 3 @iOS 10.1

override func viewWillAppear(animated: Bool){

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

runs perfect

Solution 4 - Ios

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionmyell0wView Question on Stackoverflow
Solution 1 - Iosblacky85View Answer on Stackoverflow
Solution 2 - IosChris MilesView Answer on Stackoverflow
Solution 3 - IosMarco Aurelio AmericoView Answer on Stackoverflow
Solution 4 - IosGautam JainView Answer on Stackoverflow