1

我正在尝试实施一些我卡在某一点上的东西。

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

  • 适用于 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 或更低版本中不起作用

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

谢谢

4

1 回答 1

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;

}

视图控制器

顶部:检测设备类型

#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];
}
于 2015-02-03T11:37:33.897 回答