Reputation: 175
Currently I'm using this method to detect the orientation and device:
It works for iPhones, but these's an issue with iPad, that is whatever the orientation is, [[UIScreen mainScreen] bounds].size.width
always ==768, and [[UIScreen mainScreen] bounds].size.height
always == 1024. What's wrong with my coding?
+(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;
}
}
}
}
}
By the way, in extension you don't have access to [UIApplication sharedApplication]
Upvotes: 1
Views: 479
Reputation: 236370
You should use nativeBounds instead of bounds to make sure the result doesn't depends on the device orientation of the application while it is being launched.
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 )
Upvotes: 2
Reputation: 175
I solved this problem myself, hope it will help someone.
I used xib to build the interface of the keyboard. This keyboard run both on iPhone and iPad, so I set up 4 xibs.(iPhone landscape,portrait,iPad landscape, portrait). I forgot to disable the size classes for the iPad ones. After I disabled that, it works perfectly
Upvotes: 0
Reputation: 2482
Hum, There was some changes with iOS8, Now [UIScreen bounds] is interface-oriented So the width will always be 768.
Reference:
Is [UIScreen mainScreen].bounds.size becoming orientation-dependent in iOS8?
Upvotes: 2