如何以编程方式仅为6和6Plus设备启用横向?

时间:2017-06-21 05:53:52

标签: ios objective-c xcode uiinterfaceorientation

我正在做一个iPhone项目,我需要仅为6,6 Plus和iPad提供纵向和横向支持吗?风景不适用于iPhone 4,5系列。

如何在Objective c中以编程方式执行此操作?

5 个答案:

答案 0 :(得分:1)

- (NSInteger)iPhoneType {

    struct utsname systemInfo;

    uname(&systemInfo);

    NSString *platform = [NSString stringWithCString:systemInfo.machine encoding:NSASCIIStringEncoding];

    if ([platform isEqualToString:@"iPhone1,1"]) return 1;//@"iPhone 2G";

    if ([platform isEqualToString:@"iPhone1,2"]) return 2;//@"iPhone 3G";

    if ([platform isEqualToString:@"iPhone2,1"]) return 3;//@"iPhone 3GS";

    if ([platform isEqualToString:@"iPhone3,1"]) return 4;//@"iPhone 4";

    if ([platform isEqualToString:@"iPhone3,2"]) return 4;//@"iPhone 4";

    if ([platform isEqualToString:@"iPhone3,3"]) return 4;//@"iPhone 4";

    if ([platform isEqualToString:@"iPhone4,1"]) return 5;//@"iPhone 4S";

    if ([platform isEqualToString:@"iPhone5,1"]) return 5;//@"iPhone 5";

    if ([platform isEqualToString:@"iPhone5,2"]) return 5;//@"iPhone 5";

    if ([platform isEqualToString:@"iPhone5,3"]) return 6;//@"iPhone 5c";

    if ([platform isEqualToString:@"iPhone5,4"]) return 6;//@"iPhone 5c";

    if ([platform isEqualToString:@"iPhone6,1"]) return 7;//@"iPhone 5s";

    if ([platform isEqualToString:@"iPhone6,2"]) return 7;//@"iPhone 5s";

    if ([platform isEqualToString:@"iPhone8,4"]) return 8;//@"iPhone SE";

    if ([platform isEqualToString:@"iPhone7,1"]) return 9;// @"iPhone 6 Plus";

    if ([platform isEqualToString:@"iPhone7,2"]) return 10;//@"iPhone 6";

    if ([platform isEqualToString:@"iPhone8,1"]) return 11;//@"iPhone 6s";

    if ([platform isEqualToString:@"iPhone8,2"]) return 12;//@"iPhone 6s Plus";

    if ([platform isEqualToString:@"iPhone9,1"]) return 13;//@"iPhone 7";

    if ([platform isEqualToString:@"iPhone9,2"]) return 14;//@"iPhone 7 Plus";

    if ([platform isEqualToString:@"iPod1,1"])   return 15;//@"iPod Touch 1G";

    if ([platform isEqualToString:@"iPod2,1"])   return 16;//@"iPod Touch 2G";

    if ([platform isEqualToString:@"iPod3,1"])   return 17;//@"iPod Touch 3G";

    if ([platform isEqualToString:@"iPod4,1"])   return 18;//@"iPod Touch 4G";

    if ([platform isEqualToString:@"iPod5,1"])   return 19;//@"iPod Touch 5G";

    if ([platform isEqualToString:@"iPad1,1"])   return 20;//@"iPad 1G";

    if ([platform isEqualToString:@"iPad2,1"])   return 21;//@"iPad 2";

    if ([platform isEqualToString:@"iPad2,2"])   return 22;//@"iPad 2";

    if ([platform isEqualToString:@"iPad2,3"])   return 23;//@"iPad 2";

    if ([platform isEqualToString:@"iPad2,4"])   return 24;//@"iPad 2";

    if ([platform isEqualToString:@"iPad2,5"])   return 25;//@"iPad Mini 1G";

    if ([platform isEqualToString:@"iPad2,6"])   return 26;//@"iPad Mini 1G";

    if ([platform isEqualToString:@"iPad2,7"])   return 27;//@"iPad Mini 1G";

    if ([platform isEqualToString:@"iPad3,1"])   return 28;//@"iPad 3";

    if ([platform isEqualToString:@"iPad3,2"])   return 29;//@"iPad 3";

    if ([platform isEqualToString:@"iPad3,3"])   return 30;//@"iPad 3";

    if ([platform isEqualToString:@"iPad3,4"])   return 31;//@"iPad 4";

    if ([platform isEqualToString:@"iPad3,5"])   return 32;//@"iPad 4";

    if ([platform isEqualToString:@"iPad3,6"])   return 33;//@"iPad 4";

    if ([platform isEqualToString:@"iPad4,1"])   return 34;//@"iPad Air";

    if ([platform isEqualToString:@"iPad4,2"])   return 35;//@"iPad Air";

    if ([platform isEqualToString:@"iPad4,3"])   return 36;//@"iPad Air";

    if ([platform isEqualToString:@"iPad4,4"])   return 37;//@"iPad Mini 2G";

    if ([platform isEqualToString:@"iPad4,5"])   return 38;//@"iPad Mini 2G";

    if ([platform isEqualToString:@"iPad4,6"])   return 39;//@"iPad Mini 2G";
    */
    if ([platform isEqualToString:@"i386"])      return 40;// @"iPhone Simulator";

    if ([platform isEqualToString:@"x86_64"])    return 41;//@"iPhone Simulator";

    return 0;

}

你可以通过这种方法判断。

答案 1 :(得分:0)

我们在supportedInterfaceOrientationsFor中有UIApplicationDelegate方法,因此您可以实施它并检查设备高度条件并返回所需的UIInterfaceOrientationMask,如下所示

<强>目标C

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
   // iPhone 5 height:: 568 
    if ([[UIScreen mainScreen] bounds].size.height > 568) {
        return UIInterfaceOrientationMaskAll;
    } else {
        return UIInterfaceOrientationMaskPortrait;
    }
}

<强>夫特-3

 func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
        // iPhone 5 height:: 568
        if UIScreen.main.bounds.height > 568 {
            return .all
        } else {
            return .portrait
        }
   }

答案 2 :(得分:0)

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window {
    if([[UIDevice currentDevice]userInterfaceIdiom]==UIUserInterfaceIdiomPhone) { 
         //For iPhone
         if ([[UIScreen mainScreen] bounds].size.height >= 568) {
            return UIInterfaceOrientationMaskPortrait;
         } else {
            return UIInterfaceOrientationMaskAll;
         }
    } else {
         //For iPad
         //your conditions for iPad 
         return UIInterfaceOrientationMaskAll
    }
}

答案 3 :(得分:0)

斯威夫特3:

我选择这种方式:

  

我无法访问Xcode以在Objective-C中编写它。   也许我稍后会添加Objective-C代码。

首先确定设备型号名称:

public extension UIDevice {

var modelName: String {
    var systemInfo = utsname()
    uname(&systemInfo)
    let machineMirror = Mirror(reflecting: systemInfo.machine)
    let identifier = machineMirror.children.reduce("") { identifier, element in
        guard let value = element.value as? Int8, value != 0 else { return identifier }
        return identifier + String(UnicodeScalar(UInt8(value)))
    }

    switch identifier {
    case "iPod5,1":                                 return "iPod Touch 5"
    case "iPod7,1":                                 return "iPod Touch 6"
    case "iPhone3,1", "iPhone3,2", "iPhone3,3":     return "iPhone 4"
    case "iPhone4,1":                               return "iPhone 4s"
    case "iPhone5,1", "iPhone5,2":                  return "iPhone 5"
    case "iPhone5,3", "iPhone5,4":                  return "iPhone 5c"
    case "iPhone6,1", "iPhone6,2":                  return "iPhone 5s"
    case "iPhone7,2":                               return "iPhone 6"
    case "iPhone7,1":                               return "iPhone 6 Plus"
    case "iPhone8,1":                               return "iPhone 6s"
    case "iPhone8,2":                               return "iPhone 6s Plus"
    case "iPhone9,1", "iPhone9,3":                  return "iPhone 7"
    case "iPhone9,2", "iPhone9,4":                  return "iPhone 7 Plus"
    case "iPhone8,4":                               return "iPhone SE"
    case "iPad2,1", "iPad2,2", "iPad2,3", "iPad2,4":return "iPad 2"
    case "iPad3,1", "iPad3,2", "iPad3,3":           return "iPad 3"
    case "iPad3,4", "iPad3,5", "iPad3,6":           return "iPad 4"
    case "iPad4,1", "iPad4,2", "iPad4,3":           return "iPad Air"
    case "iPad5,3", "iPad5,4":                      return "iPad Air 2"
    case "iPad2,5", "iPad2,6", "iPad2,7":           return "iPad Mini"
    case "iPad4,4", "iPad4,5", "iPad4,6":           return "iPad Mini 2"
    case "iPad4,7", "iPad4,8", "iPad4,9":           return "iPad Mini 3"
    case "iPad5,1", "iPad5,2":                      return "iPad Mini 4"
    case "iPad6,3", "iPad6,4", "iPad6,7", "iPad6,8":return "iPad Pro"
    case "AppleTV5,3":                              return "Apple TV"
    case "i386", "x86_64":                          return "Simulator"
    default:                                        return identifier
    }
}    
}

然后在AppDelegate中过滤设备方向:

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    if  UIDevice.currentDevice().modelName == "iPhone 4"  ||
        UIDevice.currentDevice().modelName == "iPhone 5"  ||
        UIDevice.currentDevice().modelName == "iPhone 4s" ||
        UIDevice.currentDevice().modelName == "iPhone 5s" {

        return .portrait
    } else {
        return .all
    }
}

答案 4 :(得分:0)

检查您的设备屏幕尺寸是否小于或等于iPhone 5 屏幕尺寸,然后仅支持纵向模式。此外,还支持更高屏幕尺寸设备的所有类型方向。在AppDelegate.m fil

中添加此代码
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {

    CGSize screenSize = [[UIScreen mainScreen] bounds].size;

    if (screenSize.height <= 568.0) {
        // If the screen size is less than or equal to iPhone 5 screen size then only Portrait mode
        return UIInterfaceOrientationMaskPortrait;
    } else {
        // Else Support all the orientation. Even apple introduced new size screen devices, which is greater then iphone 5 screen size, then this will work.
        return UIInterfaceOrientationMaskAll;
    }

}