Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在iOS8键盘扩展中检测iPad方向_Ios_Objective C_Ipad_Swift_Ios8 - Fatal编程技术网

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

如何在iOS8键盘扩展中检测iPad方向,ios,objective-c,ipad,swift,ios8,Ios,Objective C,Ipad,Swift,Ios8,目前我正在使用此方法检测方向和设备: 它适用于iPhone,但这是iPad的一个问题,即无论方向是什么,[[UIScreen mainScreen]界限].size.widthalways==768和[[UIScreen mainScreen]界限].size.height始终==1024。我的编码有什么问题 +(NSString*)GetOrientation{ if([UIScreen mainScreen].bounds.size.width < [UIScreen mai

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

它适用于iPhone,但这是iPad的一个问题,即无论方向是什么,[[UIScreen mainScreen]界限].size.widthalways==768和[[UIScreen mainScreen]界限].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

参考:


嗯,iOS8有一些变化, 现在[UIScreen bounds]是面向接口的 因此,宽度将始终为768

参考:


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


我使用xib来构建键盘的界面。这个键盘同时在iPhone和iPad上运行,所以我设置了4个XIB。iPhone横向,纵向,iPad横向,纵向。我忘了禁用iPad的大小类。在我禁用该功能后,它工作正常

我自己解决了这个问题,希望它能帮助别人

我使用xib来构建键盘的界面。这个键盘同时在iPhone和iPad上运行,所以我设置了4个XIB。iPhone横向,纵向,iPad横向,纵向。我忘了禁用iPad的大小类。在我禁用该功能后,它可以正常工作

您应该使用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 )
您应该使用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 )

我的iPhone和iPad都运行iOS8,为什么这种方法适用于我的iPhone,但不适用于我的iPad?如果它不起作用,我应该用什么来代替呢?奇怪的是,从iPad到iPhone的行为发生了变化。iPhone和iPad上的iOS 8版本相同吗?是的,它们都是iOS 8。12谢谢,我自己解决了这个问题。你的回答有道理,但并没有真正解决我的问题,因为这种情况太特殊了。我赞成你的答案,但我不能接受你的答案。很抱歉。希望你能理解我。我的iPhone和iPad都运行iOS8,为什么这种方法对我的iPhone有效,但对我的iPad无效?如果它不起作用,我应该用什么来代替呢?奇怪的是,从iPad到iPhone的行为发生了变化。iPhone和iPad上的iOS 8版本相同吗?是的,它们都是iOS 8。12谢谢,我自己解决了这个问题。你的回答有道理,但并没有真正解决我的问题,因为这种情况太特殊了。我赞成你的答案,但我不能接受你的答案。很抱歉。希望你能理解我,谢谢。我已经解决了这个问题。忘记禁用XIB的大小类导致了问题。对不起,浪费了你的时间。谢谢。我已经解决了这个问题。忘记禁用XIB的大小类导致了问题。对不起,浪费了你的时间。