"refresh" view on device rotation
Asked Answered
P

1

6

I have my view set up in viewDidLoad. All the different frames and such of the subviews have been defined relative to self.view. Therefore it doesn't matter what size self.view is the subviews will always shrink or expand to fit (as it were).

So when I rotate my device I want the view to rotate (easy enough with shouldAutoRotateToInterfaceOrientation:...) but the subviews stay the same shape.

Calling [self viewDidLoad]; makes all the elements fit, but puts a new layer on top of the previous layout (which is obvious... but i'm just saying to explain what I mean).

Is there any way to refresh the frames of the subviews or something? I don't know what other people do to be honest. Am I going to have to put ALL of my views into the .h file as properties and do everything manually on didRotate...?

Photosynthesis answered 23/9, 2010 at 11:42 Comment(0)
M
6

You have three options:

  1. If autoresizing masks are good enough to position your views, assign the correct autoresizing mask to each subview when you create them.

  2. If autoresizing masks are not sufficient, override willAnimateRotationToInterfaceOrientation:duration: and reposition your subviews in that method. I would create a custom method that takes the orientation as a parameter and is responsible for laying out all subviews. You can then call this method from willAnimateRotationToInterfaceOrientation:duration: and from viewDidLoad.

  3. You could also create a custom UIView subclass and make your view controller's view an instance of this class. Then override layoutSubviews to position all subviews depending on the view's size. This approach implies that your custom view manages its subviews instead of the view controller.

Metatherian answered 23/9, 2010 at 13:12 Comment(2)
thanks... looked into autoresizing masks :) that should do for now. :)Photosynthesis
Just to add: If you're writing your own UIView containment controller. You can override willLayoutSubviews: and didLayoutSubviews: to get notified when the device is rotating.Tuberose

© 2022 - 2024 — McMap. All rights reserved.