简体   繁体   中英

IPhonesdk Orientation Issue

I have two views, on first view i have multiple buttons, i wanted that view to be landscape right so i put this code:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
     return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

it works fine as my application starts in landscape right mode. the buttons on this view pushes user to second view which i wanted to be a portrait mode. so i put that code in second view but its not working as i wanted.

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
         return (interfaceOrientation == UIInterfaceOrientationPortrait);
    }

its showing my the orientation which i had on previous view. should i have to put something on viewdidload to make it works? please help. thanks

It's a bit a hack, but here it is. In the -(void)viewDidLoad of the viewController that you want to force portrait for do the following:

UIViewController *viewController = [[UIViewController alloc] init];
[self presentModalViewController:viewController animated:NO];
[self dismissModalViewControllerAnimated:NO];
[viewController release];

This basically forces portrait, by presenting a controller (which only supports portrait by default).

You should use one orientation for all view controllers. Otherwise you may have some problems. For example in your case imagine that the user has switched to the second view and has changed the orientation to UIInterfaceOrientationPortrait . Now he returns to the first screen and all buttons are on the wrong positions! Actually the problem is that the orientation will not change to UIInterfaceOrientationLandscapeRight back as you expect. It happens because shouldAutorotateToInterfaceOrientation is not called after viewWillAppear is triggered. So you have to code some dirty hacks to force orientation change. It is not a good solution.

By the way, how do you change your controllers? If you are using addSubview , then your second controller's shouldAutorotateToInterfaceOrientation will not be triggered at all. When using addSubview you should handle it by yourself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM