强制定位到纵向模式

时间:2013-09-05 20:54:00

标签: objective-c orientation portrait

我的所有应用程序都处于纵向模式,但我在横向模式下有一个viewcontroller是图像库。

在项目摘要选项卡上启用LandscapeLeft模式,所以我必须在ViewController的其余部分禁用旋转,除了在图像库的VC中。

我想在纵向模式下保持旋转,要执行此操作并阻止所有VC纵向,我使用以下代码

-(BOOL)shouldAutorotate{
return YES;
}

-(NSInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
}
return UIInterfaceOrientationPortrait;
}

这个,让我在之前的VC中保持横向模式,当它应该旋转到Portrait。

有什么想法吗?

1 个答案:

答案 0 :(得分:6)

对于纵向模式VC,

#pragma mark iOS 5 Orientation Support

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

     return UIInterfaceOrientationIsPortrait(interfaceOrientation);
}

#pragma mark iOS 6 Orientation Support

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

对于横向模式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;
}

如果您使用的是navigationController,

创建一个这样的类别

    @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