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

时间:2015-02-03 10:02:35

标签: iphone ios7 ios8 screen-orientation interface-orientation

我正在尝试实施一些我坚持的事情。

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

  • 适用于iPhone 4,4S,5,5S和& 6 - 仅限肖像

  • 适用于iPhone 6 Plus - 无论是肖像还是景观

  • 适用于iPad - 无论是肖像还是景观

我已经尝试了所有编码组合。最后我得到了 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或更低版​​本中不起作用

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

谢谢

1 个答案:

答案 0 :(得分:1)

简单但工作得非常好。 IOS 7.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];
}