iOS 7 停止自动旋转

标签 ios iphone

我试图将整个应用的方向锁定为纵向模式,因此我将 info.plist 更改为:

enter image description here

但是,如果我旋转模拟器甚至我的真实设备, View 仍然会旋转。对此有什么解释吗?谢谢!

最佳答案

您需要在您的 ViewController 中实现它:

对于 iOS 6 及更高版本:

#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 60000

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}

- (BOOL)shouldAutorotate
{
    return YES;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

#endif
#endif

iOS 6 以下:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    BOOL returnBool = YES;

    if (interfaceOrientation == UIInterfaceOrientationPortrait) {
        returnBool = YES;
    }
    else if (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
        returnBool = YES;
    }
    else if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
        returnBool = NO;
    }
    else if (interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
        returnBool = NO;
    }
    else {
        returnBool = NO;
    }

    return returnBool;
}

预处理器语句是为了确保方法 __IPHONE_OS_VERSION_MAX_ALLOWED 存在

#ifdef __IPHONE_OS_VERSION_MAX_ALLOWED

第二个:

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 60000

是为了确保设备上运行的 iOS 版本为 6 及以上,因为预处理器语句中的方法在 iOS 6 以下的版本上不存在。 如果没有预处理器语句,它会在 iOS 6 以下崩溃。

关于iOS 7 停止自动旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20926154/

相关文章:

ios - 阿拉伯文本的 URL 解析错误

ios - swift 自定义 .framework 找不到协议(protocol)

ios - 如何在图像 iOS 中动态突出显示单词

iphone - 以编程方式在 UIView 上添加 UITextField

c# - MonoTouch - 检测 UITableView 滚动

ios - CGContextDrawImage 内存未释放

iphone - Obj-c,在没有核心数据的情况下,执行许多 SQLite 插入/更新查询的最快方法是什么?

iOS App Store 不同账号发布

iphone - 在cocos2d中仅加载大图像的可见区域

ios - 如何使用核心数据在 iOS swift 中保存和获取 VideoURL?