CGFloat height = [UIScreen mainScreen].bounds.size.height;
NSLog(@"screen soze is %f",height);
if (height>550) {
// 4" screen-do some thing
}
else if (height<500) {
// 3.5 " screen- do some thing
}
为了解决这个问题,您可以简单地使用新的 nativeBounds 属性,而不是 bounds ,因为它基于纵向模式赢得了't change with the orientation, and as it' . 请注意, nativeBounds 的尺寸以像素为单位测量,因此对于iPhone 5,高度将为 1136 而不是568 .
Added iPad types. iPad 2 and iPad mini are non-retina iPads. While iPad Mini 2 & above, iPad 3, 4, iPad Air, Air 2, Air 3, and iPad Pro 9.7 have same logical resolution of 1024. iPad Pro has maxLength of 1366.Reference
import UIKit
public enum DisplayType {
case unknown
case iphone4
case iphone5
case iphone6
case iphone6plus
case iPadNonRetina
case iPad
case iPadProBig
static let iphone7 = iphone6
static let iphone7plus = iphone6plus
}
public final class Display {
class var width:CGFloat { return UIScreen.main.bounds.size.width }
class var height:CGFloat { return UIScreen.main.bounds.size.height }
class var maxLength:CGFloat { return max(width, height) }
class var minLength:CGFloat { return min(width, height) }
class var zoomed:Bool { return UIScreen.main.nativeScale >= UIScreen.main.scale }
class var retina:Bool { return UIScreen.main.scale >= 2.0 }
class var phone:Bool { return UIDevice.current.userInterfaceIdiom == .phone }
class var pad:Bool { return UIDevice.current.userInterfaceIdiom == .pad }
class var carplay:Bool { return UIDevice.current.userInterfaceIdiom == .carPlay }
class var tv:Bool { return UIDevice.current.userInterfaceIdiom == .tv }
class var typeIsLike:DisplayType {
if phone && maxLength < 568 {
return .iphone4
}
else if phone && maxLength == 568 {
return .iphone5
}
else if phone && maxLength == 667 {
return .iphone6
}
else if phone && maxLength == 736 {
return .iphone6plus
}
else if pad && !retina {
return .iPadNonRetina
}
else if pad && retina && maxLength == 1024 {
return .iPad
}
else if pad && maxLength == 1366 {
return .iPadProBig
}
return .unknown
}
}
If the project was created in Xcode 5 and opened in Xcode 6, then use the below mentioned code to detect the devices.(This code works if no launching images for iPhone 6,6+ are assigned)
if KRDeviceType() <= .iPhone5 { //iPhone 5/5s/SE of iPhone 4s
// Perform appropiate operations (for example, set up constraints for those old devices)
}
24 回答
在如此多的层面上依赖于大小是错误的 . 我们问系统怎么样?
取自Best way to detect hardware type, iPhone4 or iPhone5?,edzio27回答 .
这样您就可以检测设备系列 .
在Swift,iOS 8项目中,我喜欢在
UIScreen
上进行扩展,如:(注意:
nativeBounds
以像素为单位) .然后代码就像:
因此代码清楚地表明这是对主屏幕的检查,而不是对设备模型的检查 .
借用Samrat Mazumdar的答案,这是一种估算设备屏幕尺寸的简短方法 . 它适用于最新的设备,但未来可能会失败(因为所有猜测方法都可能) . 如果镜像设备也会感到困惑(返回设备的屏幕大小,而不是镜像的屏幕大小)
首先,您不应重建所有视图以适应新屏幕,也不应对不同屏幕尺寸使用不同视图 .
使用iOS的 auto-resizing 功能,以便您的视图可以调整,并适应任何屏幕大小 .
这不是很难,请阅读some documentation . 它会为你节省很多时间 .
iOS 6还提供了相关的新功能,但目前仍在NDA下 .
如果您可以访问它,请务必阅读Apple Developer网站上的API changelog .
Edit :由于iOS 6现已用完,请检查新的AutoLayout功能 .
也就是说,如果你真的需要检测iPhone 5,你可以简单地依靠screen size .
iPhone 5的屏幕高度为568 .
您可以想象一个宏,以简化所有这些:
在比较浮点时,使用
fabs
与epsilon可以防止精度错误,如H2CO3的评论中所指出的那样 .所以从现在开始,你可以在标准的if / else语句中使用它:
Edit - Better detection
正如一些人所说,这只是 detect a widescreen ,而不是真正的iPhone 5 .
iPod touch的下一个版本也可能有这样的屏幕,所以我们可能会使用另一组宏 .
让我们重命名原始宏
IS_WIDESCREEN
:让我们添加模型检测宏:
这样,我们可以确保我们有一个宽屏的iPhone型号 AND ,我们可以重新定义
IS_IPHONE_5
宏:另请注意,正如@ LearnCocos2D所述,如果应用程序未针对iPhone 5屏幕进行优化(缺少Default-568h@2x.png图像),则此宏将无法工作,因为屏幕大小仍为320x480一件事 .
我不认为这可能是一个问题,因为我不明白为什么我们想要在非优化的应用程序中检测iPhone 5 .
IMPORTANT - iOS 8 support
在iOS 8上,
UIScreen
类的bounds
属性现在反映了 device orientation .很明显,以前的代码不会开箱即用 .
为了解决这个问题,您可以简单地使用新的
nativeBounds
属性,而不是bounds
,因为它基于纵向模式赢得了't change with the orientation, and as it' .请注意,
nativeBounds
的尺寸以像素为单位测量,因此对于iPhone 5,高度将为 1136 而不是568 .如果您还要定位iOS 7或更低版本,请务必使用功能检测,因为在iOS 8之前调用
nativeBounds
会导致您的应用崩溃:您可以通过以下方式调整以前的宏:
显然,如果您需要检测iPhone 6或6 Plus,请使用相应的屏幕尺寸 .
Final note
意见和建议已纳入本文 .
感谢大家 .
我认为如果这个宏在设备和模拟器中工作应该是好的,下面是解决方案 .
针对SDK和OS的任何组合进行测试和设计:
斯威夫特
Added iPad types. iPad 2 and iPad mini are non-retina iPads. While iPad Mini 2 & above, iPad 3, 4, iPad Air, Air 2, Air 3, and iPad Pro 9.7 have same logical resolution of 1024. iPad Pro has maxLength of 1366. Reference
看到它在行动https://gist.github.com/hfossli/bc93d924649de881ee2882457f14e346
注意:如果是iPhone 6处于缩放模式,UI是iPhone 5的放大版本 . 这些功能不是确定设备类型,而是显示模式因此iPhone 5是此示例中的期望结果 .
Objective-C
用法:http://pastie.org/9687735
注意:如果是iPhone 6处于缩放模式,UI是iPhone 5的放大版本 . 这些功能不是确定设备类型,而是显示模式因此iPhone 5是此示例中的期望结果 .
真的很简单的解决方案
我们现在需要考虑iPhone 6和6Plus屏幕尺寸 . 这是一个更新的答案
一些有用的信息
我已经冒昧地将Macmade的宏放入C函数中,并将其命名为正确,因为它检测到 widescreen availability 而不一定是iPhone 5 .
该宏也不包括 Default-568h@2x.png . 如果没有新的默认图像,iPhone 5将报告常规的480x320屏幕尺寸(以磅为单位) . 因此,检查不仅适用于宽屏可用性,还适用于 widescreen mode being enabled .
这是我们的代码, test passed on ios7/ios8 for iphone4,iphone5,ipad,iphone6,iphone6p, no matter on devices or simulator:
我使用了hfossli的答案并将其翻译成Swift
这是我的cocos2d项目的宏 . 对于其他应用应该是相同的 .
我发现答案不包括模拟器的特殊情况 .
If the project is created using Xcode 6, then use the below mentioned code to detect the devices..
If the project was created in Xcode 5 and opened in Xcode 6, then use the below mentioned code to detect the devices.(This code works if no launching images for iPhone 6,6+ are assigned)
If you are still using Xcode 5 all together then use the following code to detect the devices (iPhone 6 and 6+ will not be detected)
添加'New Swift File' - >
AppDelegateEx.swift
添加扩展名
AppDelegate
已经回答了一百次,但这个解决方案对我来说是最好的 . 它是一个简单的辅助函数,不需要扩展系统类 .
Swift 3助手:
这是因为它很容易记住手机的英寸尺寸,例如“5.5英寸”或“4.7英寸”设备,但难以记住确切的像素尺寸 .
这也让你有机会做这样的事情:
如果Apple发布新的设备尺寸并且您尚未更新应用程序,则“defaultValue”可确保您的代码始终回退到安全大小 .
请注意,这是针对手机应用程序的,需要对通用应用程序进行一些更改 .
在Swift 3中,您可以使用我的简单类KRDeviceType .
https://github.com/ulian-onua/KRDeviceType
它有详细记录并支持运算符==,> =,<= .
例如,要检测设备是否具有iPhone 6 / 6s / 7的边界,您可以使用下一个比较:
要检测设备是否具有iPhone 5 / 5S / SE或更早版本(iPhone 4s)的界限,您可以使用下一个比较:
使用以下代码:
这是设备的正确测试,不依赖于方向
用于检测所有版本的iPhone和iPad设备 .