Force orientation to Portrait Mode

北城以北 提交于 2019-12-02 13:12:13

问题


I have all the app in portrait mode, but I have one of the viewcontroller in landscape mode to be a gallery of images.

On the tab Project Summary enable LandscapeLeft mode, so I have to disable the rotation this way in the rest of the Viewcontroller, except in the VC of the image gallery.

I want to keep the rotation in Portrait mode, to do this and to block all the VC portrait, I used the following code

-(BOOL)shouldAutorotate{
return YES;
}

-(NSInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
}
return UIInterfaceOrientationPortrait;
}

This, keep me the landscape mode in the previous VC , when it should be rotated to Portrait.

Any idea do?


回答1:


For Portrait Mode VC,

#pragma mark iOS 5 Orientation Support

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

     return UIInterfaceOrientationIsPortrait(interfaceOrientation);
}

#pragma mark iOS 6 Orientation Support

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

For Landscape mode VC,

#pragma mark - iOS 5.0 and up Rotation Methods

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

    return UIInterfaceOrientationMaskLandscape;

}

#pragma mark - iOS 6.0 and up Rotation Methods

- (NSUInteger)supportedInterfaceOrientations;
{
    return UIInterfaceOrientationMaskLandscape;
}

If you are using navigationController,

Create a category like this,

    @interface UINavigationController (Rotation_IOS6)

    @end

    @implementation UINavigationController (Rotation_IOS6)

    -(BOOL)shouldAutorotate
    {
        if([self.visibleViewController isMemberOfClass:NSClassFromString(@"YourLandscapeViewController")])
        {
            return UIInterfaceOrientationMaskLandscape
        }
        return NO;
    }

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

    - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
    {
        if([self.visibleViewController isMemberOfClass:NSClassFromString(@"YourLandscapeViewController")])
        {
            return UIInterfaceOrientationMaskLandscape
        }
        return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
    }

@end


来源:https://stackoverflow.com/questions/18645782/force-orientation-to-portrait-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!