A view controller is in landscape mode, but I'm getting the frame from portrait mode?

北战南征 提交于 2019-12-03 08:31:42

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

Screen rotation is only done to the top view (controller) in your hierarchy and rotation messages are forwarded through container-VCs that support this behavior (e.g. UINavigationController, UITabBarController).

If you want your view to be sized correctly, use [UIView initWithFrame:self.view.bounds] for creating a view. Insinde UIViewController you only have the chance to override the loadView Method to set the frame before viewDidLoad gets called (or you could use a different XIB for landscape). You could also hardcode the view's size by setting its frame first thing in viewDidLoad

You can use viewWillAppear to size your views before they display

You have to implement this in your settings view controller:-

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!