Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"refresh" view on device rotation

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...?

like image 900
Thomas Clayson Avatar asked Sep 23 '10 11:09

Thomas Clayson


People also ask

How do I get my screen to rotate again?

Open your device's Settings app. . Select Accessibility. Select Auto-rotate screen.

Why did my screen stop rotating?

Cause of Android Screen Not Rotating Any of the below issues can prevent your screen to rotate properly when you turn your phone. Auto rotate option is turned off or not working. The screen you're using isn't set to auto-rotate. Recent apps are interfering with auto-rotate.


1 Answers

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.

like image 159
Ole Begemann Avatar answered Sep 23 '22 15:09

Ole Begemann