开发者

Fixing the orientation of view controllers

开发者 https://www.devze.com 2023-01-16 14:17 出处:网络
I have a button in my RootViewController .... when i click on this button it will navigate to some otherViewController.

I have a button in my RootViewController .... when i click on this button it will navigate to some otherViewController. but i want that when i click on the Button otherViewController's Orientation should be Landscape &开发者_如何学运维amp; when i back from this page(otherViewController) i should got again Portrait mode.

lots of Thanks for any help. Plz help me i m new in Iphone so can't able to handle this.


Note in iOS 6 shouldAutorotateToInterfaceOrientation is deprecated. Instead you should use supportedInterfaceOrientations and return a UIInterfaceOrientationMask.

So for example:

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape;
}

Here are all the UIInterfaceOrientationMask options you might return:

UIInterfaceOrientationMaskAll
UIInterfaceOrientationMaskAllButUpsideDown
UIInterfaceOrientationMaskLandscape
UIInterfaceOrientationMaskLandscapeLeft
UIInterfaceOrientationMaskLandscapeRight
UIInterfaceOrientationMaskPortrait
UIInterfaceOrientationMaskPortraitUpsideDown

You should also consider the preferredInterfaceOrientationForPresentation.


In general, orientation should only change from portrait to landscape (or vice versa) when the user rotates the device.

However, if you want your RootViewController to always present its view in portrait and your OtherViewController to always present itvs view in landscape, that's easy enough.

In your RootViewController's .h:

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

In your OtherViewController's .h:

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

Although this would probably be better as it would allow both landscape-left and landscape-right rotations:

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


The only way I am aware this can be done is by presenting otherViewController modally.

See my answer here: UIViewController loads rotated to landscape but only supports portrait


Maybe it helps.

in viewWillAppear of your new view controller add

[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscape]

and in viewWillAppear of your root viewController add

[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];

It will cause warning but it works.

0

精彩评论

暂无评论...
验证码 换一张
取 消