【问题标题】:How to detect iPad orientation in iOS8 keyboard extension如何在 iOS8 键盘扩展中检测 iPad 方向
【发布时间】:2015-03-16 08:17:10
【问题描述】:

目前我正在使用这种方法来检测方向和设备:

它适用于 iPhone,但这是 iPad 的问题,无论方向是什么,[[UIScreen mainScreen] bounds].size.widthalways ==768,[[UIScreen mainScreen] bounds].size.height always == 1024。我的编码有什么问题?

 +(NSString*)GetOrientation{
    if([UIScreen mainScreen].bounds.size.width < [UIScreen mainScreen].bounds.size.height){
        NSLog(@"Potrait");
        return @"Potrait";
    }
    else{
        NSLog(@"Landscape");
        return @"Landscape";
    }


}
+(NSString*)GetDeviceAccordingToScreenSize:(UIViewController*)ctrl{
    if ([[UIDevice currentDevice].model isEqualToString:@"iPad"]) {
        int screenWidth = [[UIScreen mainScreen] bounds].size.width;
        if (screenWidth==768) {
            return @"P-iPad";

        }else if(screenWidth==1024){
            return @"L-iPad";

        }else{
        return @"ERROR";
        }
    }else{
        if ([[self GetOrientation] isEqualToString:@"Potrait"]) {
            int screenHeight = [[UIScreen mainScreen] bounds].size.height;
            switch (screenHeight) {
                case 667:{
                    return @"P-iPhone6";
                    break;
                }

                case 736:{
                    return @"P-iPhone6Plus";
                    break;
                }
                case 568:{
                    return @"P-iPhone5";

                }
                default:{
                    return @"P-iPhone4";
                    break;
                }
            }
        }else{
            int screenWidth = [[UIScreen mainScreen] bounds].size.width;
            // float screenHeight = [[UIScreen mainScreen] bounds].size.height;
            switch (screenWidth) {
                case 667:{
                    return @"L-iPhone6";
                    break;
                }

                case 736:{
                    return @"L-iPhone6Plus";
                    break;
                }
                case 568:{

                    return @"L-iPhone5";
                    break;
                }
                default:{
                    return @"L-iPhone4";
                    break;
                }
            }

        }

    }

}

顺便说一句,在扩展中你无权访问 [UIApplication sharedApplication]

【问题讨论】:

    标签: ios objective-c ipad swift ios8


    【解决方案1】:

    您应该使用 nativeBounds 而不是边界,以确保结果不依赖于应用程序启动时的设备方向。

    extension UIDevice{
        var detail:String {
            if iPhone {
                if UIScreen.mainScreen().nativeBounds.height == 480 {
                    return "iPhone Classic"
                }
                if UIScreen.mainScreen().nativeBounds.height == 960 {
                    return "iPhone 4 or 4S"
                }
                if UIScreen.mainScreen().nativeBounds.height == 1136 {
                    return "iPhone 5 or 5S or 5C"
                }
                if UIScreen.mainScreen().nativeBounds.height == 1334 {
                    return "iPhone 6"
                }
                if UIScreen.mainScreen().nativeBounds.height == 2208 {
                    return "iPhone 6+"
                }
            } else if iPad {
                if UIScreen.mainScreen().nativeBounds.height == 1024 {
                    return "iPad Classic"
                }
                if UIScreen.mainScreen().nativeBounds.height == 2048 {
                    return "iPad Retina"
                }
            } else {
                return "Undefined"
            }
    
            return "test"
        }
        var iPhone:Bool {
            return UIDevice.currentDevice().userInterfaceIdiom == .Phone
        }
        var iPad:Bool {
            return UIDevice.currentDevice().userInterfaceIdiom == .Pad
        }
        var width:CGFloat{
            return UIScreen.mainScreen().bounds.width
        }  
        var landscape:Bool {
            if iPad && ( width == 1024.0 || width == 2048.0 ) {
                return true
            }
            if iPhone && ( width == 480.0 || width == 960 || width == 1136.0 || width == 1334.0 || width == 2208.0 ) {
                return true
            }
            return false
        }
    }
    if UIDevice().iPhone {
       println("This device is an iPhone")
    }
    if UIDevice().iPad {
        println("This device is an iPad")
    }
    println("Device detail: " + UIDevice().detail )
    println("Landscape: " + UIDevice().landscape.description )
    

    【讨论】:

    • 谢谢。我已经解决了这个问题。忘记禁用 xibs 的大小类导致了问题。很抱歉浪费了您的时间。
    【解决方案2】:

    嗯,iOS8有一些变化, 现在 [UIScreen bounds] 是面向接口的 所以宽度总是768。

    参考:

    Is [UIScreen mainScreen].bounds.size becoming orientation-dependent in iOS8?

    【讨论】:

    • 我的 iPhone 和 iPad 都在运行 iOS8,为什么这种方法适用于我的 iPhone,但不适用于我的 iPad?如果它不起作用,我应该改用什么?
    • 奇怪的是行为从 iPad 到 iPhone 的变化。您的 iPhone 和 iPad 上的 iOS 8 版本是否相同?
    • 是的,它们都是 iOS 8.12
    • 谢谢,我自己解决了这个问题。你的回答很有道理,但并没有真正解决我的问题,因为这种情况太特殊了。我对你的答案投了赞成票,但我不能接受你的答案。对于那个很抱歉。希望你能理解我。
    【解决方案3】:

    我自己解决了这个问题,希望对某人有所帮助。

    我使用xib来构建键盘的界面。这个键盘可以在 iPhone 和 iPad 上运行,所以我设置了 4 个 xib。(iPhone 横向、纵向、iPad 横向、纵向)。我忘了禁用 iPad 的尺码等级。在我禁用它之后,它可以完美运行

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-22
      • 1970-01-01
      相关资源
      最近更新 更多