The view controller is in landscape mode, but am I getting a frame from portrait mode?

I have a settings view controller with a table view. It is assumed that in the table view, its frame will be a frame of the view controller. My application only supports landscape mode, so I always have orientation.

However, when the settings view controller is loaded, the table view has the portrait mode of the view controller, even if it is in landscape (360 by 480, when it should be 480 by 360).

In addition, when I checked whether the device thought it was in portrait mode, it would return.

What causes the problem and how to fix it?

+4
objective-c tableview uiinterfaceorientation viewcontroller
Oct 03 2018-11-11T00:
source share
3 answers

when you call [UIView init] , it will by default use the screen size of your device in portrait mode (the same goes for iPad, but with a larger size). Inside [UIViewController viewDidLoad] self.view will also be the default for this size.

Screen rotation is performed only for the top view (controller) in your hierarchy, and rotation messages are sent through VC containers that support this behavior (for example, UINavigationController, UITabBarController).

If you want your view to be properly configured, use [UIView initWithFrame:self.view.bounds] to create the view. Insinde UIViewController you have the option to override the loadView method to set the frame before calling viewDidLoad (or you can use a different XIB for the landscape). You can also tightly adjust the size of the view by setting its frame first in viewDidLoad

+4
Oct 03 2018-11-11T00:
source share

You can use viewWillAppear to size your views before displaying them.

0
Feb 06 '12 at 15:03
source share

You must implement this in your settings view controller: -

 -(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait]; } 
-2
Oct 03 2018-11-11T00:
source share



All Articles