質問

I have one application in which a tab bar controller is having 3 tabs and each tab having a view controller. View controller 1 and 2 are supporting only portrait mode but view controller in tab 3 supports all orientations -portrait, landscape left, landscape right and portrait upside down. For tab 3, We have to show a particular view when device is in portrait mode and another view when device is in landscape mode.

If device is in portrait mode and user clicks on the tab 3, view is loaded correctly in portrait mode and then if we rotate the device to landscape mode the landscape view is loaded correctly.

But it we turn the device to landscape mode in the tab 1 itself and then click on the tab 3 , then the problem occurs then it shows the screen to be shown in landscape mode but it displays it as a portrait view.

When I tried to find out the reason for this in the delegate method by NSLogging

(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

the value of the interfaceOrientation is 1 which is UIInterfaceOrientationPortrait and the control is going into the if condition written for the portrait mode

if(interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown){

and when I tried to print the value of UIDevice orientation in the same delegate method

UIDeviceOrientation interfaceOrientation = [UIDevice currentDevice].orientation;
NSLog(@" device orientation %u", interfaceOrientation);

the value printed in console is 4- which is UIDeviceOrientationLandscapeRight.

So the interface orientation is UIInterfaceOrientationPortrait but device orientation is UIDeviceOrientationLandscapeRight.

One more thing, now if I rotate the device to portrait mode and then moving to landscape mode shows the correct landscape view and app starts behaving correctly.

So the problem is if I load the tab 3 in landscape mode it doesn't load properly but if we enter in portrait mode and then rotate to landscape if works fine from thereon.

If anyone can give some useful suggestion why the behavior is like this that would be really beneficial.

Thanks

役に立ちましたか?

解決

Add these lines to your Tab1 & Tab2 in viewWillAppear method,

#import <objc/message.h>

- (void)viewWillAppear:(BOOL)animated {
    if(UIDeviceOrientationIsLandscape(self.interfaceOrientation)){
        if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
            objc_msgSend([UIDevice currentDevice], @selector(setOrientation:), UIInterfaceOrientationPortrait );
        }
    }
}

他のヒント

Create a CustomTabBarController Class with Subclass of UITabBarController (for e.g: CustomTabBarController)

@interface CustomTabBarController : UITabBarController

Add the following lines to this class :

- (BOOL)shouldAutorotate
{
    return [self.selectedViewController shouldAutorotate];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    return [self.selectedViewController shouldAutorotateToInterfaceOrientation:toInterfaceOrientation];
}

- (NSUInteger)supportedInterfaceOrientations
{
    return [self.selectedViewController supportedInterfaceOrientations];
}

In your app delegate or where ever you are initializing UITabBarController, replace those instances with CustomTabBarController instances.

self.tabBarController = [[[CustomTabBarController alloc] init] autorelease];

Add the lines to your model view controllers with different view controller support:

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationMaskPortrait;
}

-(BOOL)shouldAutorotate {
    return NO;
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top