iOS- 在 viewDidLoad 上检测当前大小类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29528661/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS- Detect current size classes on viewDidLoad
提问by user3193307
I'm working with adaptive Layout on iOS 8 and I want to get exactly what the size classes are on viewDidLoad
. Any ideas about that?
我正在使用 iOS 8 上的自适应布局,我想确切地了解尺寸类是什么viewDidLoad
。对此有什么想法吗?
回答by Bamsworld
As of iOS 8 UIViewControlleradopts the UITraitEnvironmentprotocol. This protocol declares a property named traitCollectionwhich is of type UITraitCollection. You can therefor access the traitCollectionproperty simply by using self.traitCollection
从 iOS 8 UIViewController采用UITraitEnvironment协议。该协议声明了一个名为traitCollection的属性,它是UITraitCollection类型。因此,您只需使用即可访问traitCollection属性self.traitCollection
UITraitCollectionhas two properties that you want to access named horizontalSizeClassand verticalSizeClassAccessing these properties return an NSInteger. The enum that defines the returned values is declared in official documentation as follows- (this could potentially be added to in the future!)
UITraitCollection有两个你想要访问的属性,名为horizontalSizeClass和verticalSizeClass访问这些属性会返回一个NSInteger。定义返回值的枚举在官方文档中声明如下 - (这可能会在未来添加!)
typedef NS_ENUM (NSInteger, UIUserInterfaceSizeClass {
UIUserInterfaceSizeClassUnspecified = 0,
UIUserInterfaceSizeClassCompact = 1,
UIUserInterfaceSizeClassRegular = 2,
};
So you could get the class and use say a switch to determine your code direction. An example could be -
所以你可以得到这个类并使用一个开关来确定你的代码方向。一个例子可能是——
NSInteger horizontalClass = self.traitCollection.horizontalSizeClass;
NSInteger verticalCass = self.traitCollection.verticalSizeClass;
switch (horizontalClass) {
case UIUserInterfaceSizeClassCompact :
// horizontal is compact class.. do stuff...
break;
case UIUserInterfaceSizeClassRegular :
// horizontal is regular class.. do stuff...
break;
default :
// horizontal is unknown..
break;
}
// continue similarly for verticalClass etc.
回答by Jon Vogel
Some useful stuff for Swift 4:
Swift 4 的一些有用的东西:
UIViewController Extension to get the classes back as a Tuple.
UIViewController 扩展以将类作为元组返回。
extension UIViewController {
func sizeClass() -> (UIUserInterfaceSizeClass, UIUserInterfaceSizeClass) {
return (self.traitCollection.horizontalSizeClass, self.traitCollection.verticalSizeClass)
}
}
Example Switch statement to consume the function:
使用函数的 Switch 语句示例:
switch self.sizeClass() {
case (UIUserInterfaceSizeClass.unspecified, UIUserInterfaceSizeClass.unspecified):
print("Unknown")
case (UIUserInterfaceSizeClass.unspecified, UIUserInterfaceSizeClass.compact):
print("Unknown width, compact height")
case (UIUserInterfaceSizeClass.unspecified, UIUserInterfaceSizeClass.regular):
print("Unknown width, regular height")
case (UIUserInterfaceSizeClass.compact, UIUserInterfaceSizeClass.unspecified):
print("Compact width, unknown height")
case (UIUserInterfaceSizeClass.regular, UIUserInterfaceSizeClass.unspecified):
print("Regular width, unknown height")
case (UIUserInterfaceSizeClass.regular, UIUserInterfaceSizeClass.compact):
print("Regular width, compact height")
case (UIUserInterfaceSizeClass.compact, UIUserInterfaceSizeClass.compact):
print("Compact width, compact height")
case (UIUserInterfaceSizeClass.regular, UIUserInterfaceSizeClass.regular):
print("Regualr width, regular height")
case (UIUserInterfaceSizeClass.compact, UIUserInterfaceSizeClass.regular):
print("Compact width, regular height")
}
Edit/Addition:
编辑/添加:
If you are trying to access the trait collection early in the UIViewController
's lifecycle they might all be UIUserInterfaceSizeClass.unspecified
.
如果您试图在UIViewController
生命周期的早期访问特征集合,它们可能都是UIUserInterfaceSizeClass.unspecified
。
This can be a pain if you happen to do constraints in code.
如果您碰巧在代码中进行约束,这可能会很痛苦。
I recommend access the .traitCollection
from the UIScreen
shared object.
我建议.traitCollection
从UIScreen
共享对象访问。
UIScreen.main.traitCollection
Or even more useful:
或者更有用:
UIScreen.main.traitCollection.userInterfaceIdiom
回答by coco
This is nice for testing/debugging:
这对于测试/调试非常有用:
let sizeClasses = ["Unspecified", "Compact", "Regular"]
print("SizeClass w:\(sizeClasses[traitCollection.horizontalSizeClass.rawValue]) h:\(sizeClasses[traitCollection.verticalSizeClass.rawValue])")
回答by Reimond Hill
You can also do it like that in Swift 5
你也可以在 Swift 5 中这样做
enum DeviceTraitStatus {
///IPAD and others: Width: Regular, Height: Regular
case wRhR
///Any IPHONE Portrait Width: Compact, Height: Regular
case wChR
///IPHONE Plus/Max Landscape Width: Regular, Height: Compact
case wRhC
///IPHONE landscape Width: Compact, Height: Compact
case wChC
static var current:DeviceTraitStatus{
switch (UIScreen.main.traitCollection.horizontalSizeClass, UIScreen.main.traitCollection.verticalSizeClass){
case (UIUserInterfaceSizeClass.regular, UIUserInterfaceSizeClass.regular):
return .wRhR
case (UIUserInterfaceSizeClass.compact, UIUserInterfaceSizeClass.regular):
return .wChR
case (UIUserInterfaceSizeClass.regular, UIUserInterfaceSizeClass.compact):
return .wRhC
case (UIUserInterfaceSizeClass.compact, UIUserInterfaceSizeClass.compact):
return .wChC
default:
return .wChR
}
}
}
The main advantatge is that can be used from not only a UIViewController class dependant and the static method can go into an i.e. Helper class. So you can do somewhere in your code:
主要优点是不仅可以从依赖的 UIViewController 类使用,而且静态方法可以进入 ie Helper 类。因此,您可以在代码中的某处执行以下操作:
let textSize:CGFloat = DeviceTraitStatus.current == .wRhR ? 18:14
回答by Treverp
So for Auto Layout you design your apps' UI per each different size class. The OS does all the work of figuring out the device being used and what size class should be used. However, if you there is a way to figure out what device is being used. I am not sure if you can decided what size class gets used as, again, this is handled dynamically by the OS.
因此,对于自动布局,您可以根据每个不同的尺寸类别设计应用程序的 UI。操作系统完成所有确定正在使用的设备以及应该使用什么尺寸等级的工作。但是,如果您有办法确定正在使用的设备。我不确定您是否可以决定使用什么大小类,因为这又是由操作系统动态处理的。
Code for getting device being used:
获取正在使用的设备的代码:
NSString *device = [[UIDevice currentDevice]model ] ;
NSLog(@"%@",device);