在 iOS 7 和 iOS 8 中限制界面方向

Restrict Interface Orientation in iOS 7 and iOS 8

我正在尝试实现我在某一点上卡住的东西。

我想要一些方向限制如下:

所有的编码组合我都试过了。最后我得到了 iOS 8

的解决方案
**(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window  // iOS 6 autorotation fix
{**


        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){

            if ([[UIScreen mainScreen] respondsToSelector: @selector(scale)])
            {
                if(IS_IPHONE_4_AND_OLDER){

                    printf("Device Type : iPhone 4,4s ");
                    return UIInterfaceOrientationMaskPortrait;
                }
                else if(IS_IPHONE_5){

                    printf("Device Type : iPhone 5,5S/iPod 5 ");
                    return UIInterfaceOrientationMaskPortrait;
                }
                else if(IS_IPHONE_6){

                    printf("Device Type : iPhone 6 ");
                    return UIInterfaceOrientationMaskPortrait;
                }
                else if(IS_IPHONE_6P){

                    printf("Device Type : iPhone 6+ ");
                    return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortrait;
                }
            }
            return UIInterfaceOrientationMaskPortrait;
        }
        else{

            printf("Device Type : iPad");
            return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortrait;
        }
}

通过使用上面的代码,我管理 iOS 8 但它在 iOS 7 或更少

中不起作用

请帮我解决这个问题...

谢谢

简单但效果很好。 IOS7.1 和 8

AppDelegate.h

@property () BOOL restrictRotation;

AppDelegate.m

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {

if(self.restrictRotation)

    return UIInterfaceOrientationMaskPortrait;
else

    return UIInterfaceOrientationMaskAll;

}

ViewController

顶部:检测设备类型

#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)

#define IS_IPHONE_5_IOS7 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 568.0f)
#define IS_IPHONE_6_IOS7 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 667.0f)
#define IS_IPHONE_6P_IOS7 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 736.0f)
#define IS_IPHONE_4_AND_OLDER_IOS7 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height < 568.0f)

#define IS_IPHONE_5_IOS8 (IS_IPHONE && ([[UIScreen mainScreen] nativeBounds].size.height/[[UIScreen mainScreen] nativeScale]) == 568.0f)
#define IS_IPHONE_6_IOS8 (IS_IPHONE && ([[UIScreen mainScreen] nativeBounds].size.height/[[UIScreen mainScreen] nativeScale]) == 667.0f)
#define IS_IPHONE_6P_IOS8 (IS_IPHONE && ([[UIScreen mainScreen] nativeBounds].size.height/[[UIScreen mainScreen] nativeScale]) == 736.0f)
#define IS_IPHONE_4_AND_OLDER_IOS8 (IS_IPHONE && ([[UIScreen mainScreen] nativeBounds].size.height/[[UIScreen mainScreen] nativeScale]) < 568.0f)

#define IS_IPHONE_5 ( ( [ [ UIScreen mainScreen ] respondsToSelector: @selector( nativeBounds ) ] ) ? IS_IPHONE_5_IOS8 : IS_IPHONE_5_IOS7 )
#define IS_IPHONE_6 ( ( [ [ UIScreen mainScreen ] respondsToSelector: @selector( nativeBounds ) ] ) ? IS_IPHONE_6_IOS8 : IS_IPHONE_6_IOS7 )
#define IS_IPHONE_6P ( ( [ [ UIScreen mainScreen ] respondsToSelector: @selector( nativeBounds ) ] ) ? IS_IPHONE_6P_IOS8 : IS_IPHONE_6P_IOS7 )
#define IS_IPHONE_4_AND_OLDER ( ( [ [ UIScreen mainScreen ] respondsToSelector: @selector( nativeBounds ) ] ) ? IS_IPHONE_4_AND_OLDER_IOS8 : IS_IPHONE_4_AND_OLDER_IOS7 )

然后在下面添加函数:

-(void) restrictRotation:(BOOL) restriction{

AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
appDelegate.restrictRotation = restriction;}

viewDidLoad

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){

    if ([[UIScreen mainScreen] respondsToSelector: @selector(scale)])
    {
        if(IS_IPHONE_6P){

            printf("Device Type : iPhone 6+ ");
            [self restrictRotation:NO];
        }
        else{

            printf("Device Type : iPhone 6 ");
            [self restrictRotation:YES];
        }
    }
}
else{

    printf("Device Type : iPad");
    [self restrictRotation:NO];
}