IPad modal view controller, acting on a portrait, even if it is a landscape

It's hard for me to imagine ModalViewController in landscape mode. Basically, it is displayed in the correct orientation of the interface and rotates only when the device is held in landscape mode, but the viewcontroller acts as if it is in portrait mode.

I represent the modal style

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self.viewController]; self.window.rootViewController = navController; [self.window makeKeyAndVisible]; LoginViewController *lvc = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:[NSBundle mainBundle]]; [self.window.rootViewController presentModalViewController:lvc animated:NO]; return YES; } 

In LoginViewController

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations NSLog(@"%@",NSStringFromCGRect(self.view.frame)); return UIInterfaceOrientationIsLandscape(interfaceOrientation); } 

The results of the magazine after several revolutions:

 2012-03-06 13:51:49.308 OrientationTest[3132:207] {{0, 0}, {1024, 748}} 2012-03-06 13:51:49.310 OrientationTest[3132:207] {{0, 0}, {1024, 748}} 2012-03-06 13:51:49.310 OrientationTest[3132:207] {{0, 0}, {1024, 748}} 2012-03-06 13:51:49.310 OrientationTest[3132:207] {{0, 0}, {1024, 748}} 2012-03-06 13:51:49.313 OrientationTest[3132:207] {{0, 20}, {1024, 748}} 2012-03-06 13:51:49.314 OrientationTest[3132:207] {{0, 0}, {748, 1024}} 2012-03-06 13:51:49.315 OrientationTest[3132:207] {{20, 0}, {748, 1024}} 2012-03-06 13:51:50.991 OrientationTest[3132:207] {{20, 0}, {748, 1024}} 2012-03-06 13:51:50.991 OrientationTest[3132:207] {{20, 0}, {748, 1024}} 2012-03-06 13:51:51.647 OrientationTest[3132:207] {{20, 0}, {748, 1024}} 2012-03-06 13:51:51.648 OrientationTest[3132:207] {{0, 0}, {748, 1024}} 2012-03-06 13:51:53.481 OrientationTest[3132:207] {{0, 0}, {748, 1024}} 2012-03-06 13:51:53.482 OrientationTest[3132:207] {{0, 0}, {748, 1024}} 2012-03-06 13:51:53.897 OrientationTest[3132:207] {{0, 0}, {748, 1024}} 2012-03-06 13:51:53.898 OrientationTest[3132:207] {{20, 0}, {748, 1024}} 

Basically loginViewController acts as if it were in portrait mode. I have two text fields in this view, and when I click one of them, I want to move the view up so that the keyboard does not appear above the text field. To do this, I need to change frame.origin.x instead of y, because the axis is inverted (the view acts like a portrait), and this causes a lot of problems.

Edit:

If I change the modal presentation style on LoginViewController to UIModalPresentationPageSheet, it works as intended, so there is a problem only with full screen mode

Second edit:

I split the code into the basics. I don’t even imagine the modal view controller anymore, just by initializing this view controller as the root view controller, and yet this happens.

Application Delegate:

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; self.window.rootViewController = self.viewController; [self.window makeKeyAndVisible]; return YES; } 

In my opinion, the controller:

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations NSLog(@"%@",NSStringFromCGRect( self.view.frame)); return UIInterfaceOrientationIsLandscape(interfaceOrientation); } 
+4
source share
3 answers

Ok, so I finally found a way to work correctly. It seems if you just create a new view controller and imagine that it will always be inverted. The fix I found is to put the view controller in the navigation controller.

 LoginViewController *lvc = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:[NSBundle mainBundle]]; UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:lvc]; nvc.navigationBarHidden = YES; lvc.modalPresentationStyle = UIModalPresentationFullScreen; lvc.parentView = self.navController.view; [self.window.rootViewController presentModalViewController:nvc animated:NO]; 
+10
source

The frame inverted x / y is a "function";) you do not have enough convertRect:fromView . See the conversion of rectangles and dots between views.

In your case, you need to convert the rectangle of the keyboard frame into the main view of the view controller. Without seeing your keyboard frame, I don’t know for sure, but maybe something like this:

 [self.view convertRect:keyboardFrame fromView:keyboardView]; 
0
source

change this value

  - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations NSLog(@"%@",NSStringFromCGRect(self.view.frame)); return UIInterfaceOrientationIsLandscape(interfaceOrientation); } 

in that

  - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations NSLog(@"%@",NSStringFromCGRect(self.view.frame)); return YES; } 
-3
source

Source: https://habr.com/ru/post/1399999/


All Articles