iOS-检测viewDidLoad上的当前大小类

时间:2015-04-09 02:22:24

标签: ios size-classes

我在iOS 8上使用自适应布局,我希望得到viewDidLoad上的大小类。有关于此的任何想法吗?

5 个答案:

答案 0 :(得分:66)

从iOS 8开始, UIViewController 采用 UITraitEnvironment 协议。此协议声明名为 traitCollection 的属性,其类型为 UITraitCollection 。您只需使用self.traitCollection

即可访问 traitCollection 属性

UITraitCollection 有两个要访问的属性,名为 horizo​​ntalSizeClass verticalSizeClass 访问这些属性会返回 NSInteger 。定义返回值的枚举在官方文档中声明如下 - (这可能会在将来添加!)

typedef NS_ENUM (NSInteger, UIUserInterfaceSizeClass {
   UIUserInterfaceSizeClassUnspecified = 0,
   UIUserInterfaceSizeClassCompact     = 1,
   UIUserInterfaceSizeClassRegular     = 2,
};

所以你可以上课并使用说开关来确定你的代码方向。一个例子可能是 -

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.

答案 1 :(得分:13)

Swift 4的一些有用的东西:

UIViewController扩展以将类作为元组返回。

extension UIViewController {
  func sizeClass() -> (UIUserInterfaceSizeClass, UIUserInterfaceSizeClass) {
      return (self.traitCollection.horizontalSizeClass, self.traitCollection.verticalSizeClass)
  }
}

使用函数的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")
    }

编辑/增加:

如果您尝试在UIViewController生命周期的早期访问特征集,则可能都是UIUserInterfaceSizeClass.unspecified

如果碰巧在代码中做约束,这可能会很痛苦。

我建议您从.traitCollection共享对象访问UIScreen

UIScreen.main.traitCollection

甚至更有用:

UIScreen.main.traitCollection.userInterfaceIdiom

答案 2 :(得分:2)

这非常适合测试/调试:

let sizeClasses = ["Unspecified", "Compact", "Regular"]
print("SizeClass w:\(sizeClasses[traitCollection.horizontalSizeClass.rawValue]) h:\(sizeClasses[traitCollection.verticalSizeClass.rawValue])")

答案 3 :(得分:0)

您也可以在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

        }

    }

}

主要优点是,不仅可以从依赖于UIViewController的类中使用它,而且可以将静态方法引入到一个Helper类中。因此,您可以在代码中的某处进行操作:

let textSize:CGFloat = DeviceTraitStatus.current == .wRhR ? 18:14

答案 4 :(得分:-10)

因此,对于自动布局,您可以设计应用程序'每个不同大小的类的UI。操作系统完成了确定所使用设备以及应使用的大小类的所有工作。但是,如果您有办法找出正在使用的设备。我不确定你是否可以决定使用什么大小的类,再次,这是由操作系统动态处理的。

获取设备的代码:

NSString *device = [[UIDevice currentDevice]model ] ;
NSLog(@"%@",device);