myell0w myell0w - 3 months ago 59
iOS Question

How to force layoutSubviews of UIView.

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?



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