繁体   English   中英

如何在iOS8键盘扩展中检测iPad方向

[英]How to detect iPad orientation in iOS8 keyboard extension

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

它适用于iPhone,但iPad遇到了这些问题,无论方向如何, [[UIScreen mainScreen] bounds].size.width总是== 768, [[UIScreen mainScreen] bounds].size.height总是== 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]

嗡嗡声,iOS8进行了一些更改,现在[UIScreen bounds]面向界面,因此宽度始终为768。

参考:

[UIScreen mainScreen] .bounds.size在iOS8中是否与方向相关?

您应该使用nativeBounds而不是bounds来确保结果在启动时不取决于应用程序的设备方向。

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 )

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

我使用xib构建键盘的界面。 该键盘可同时在iPhone和iPad上运行,因此我设置了4个xib。(iPhone横向,纵向,iPad横向,纵向)。 我忘了禁用iPad的尺寸等级。 我禁用它后,它可以正常工作

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM