-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLayoutTrait.swift
170 lines (140 loc) · 5.31 KB
/
LayoutTrait.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//
// LayoutTrait.swift
// DoudouApp
//
// Created by yushuyi on 15/7/23.
// Copyright © 2015年 DoudouApp. All rights reserved.
//
import UIKit
@available(iOS 8.0, *)
extension UITraitCollection {
//MARK: Width Compact
func wCompact_hCompact() -> Bool {
return self.horizontalSizeClass == .Compact && self.verticalSizeClass == .Compact
}
func wCompact_hRegular () -> Bool {
return self.horizontalSizeClass == .Compact && self.verticalSizeClass == .Regular;
}
func wCompact_hAny () -> Bool {
return self.horizontalSizeClass == .Compact;
}
//MARK: Width Regular
func wRegular_hRegular () -> Bool {
return self.horizontalSizeClass == .Regular && self.verticalSizeClass == .Regular;
}
func wRegular_hCompact () -> Bool {
return self.horizontalSizeClass == .Regular && self.verticalSizeClass == .Compact;
}
func wRegular_hAny () -> Bool {
return self.horizontalSizeClass == .Regular;
}
func wAny_hAny () -> Bool {
return true;
}
}
@available(iOS 8.0, *)
extension UITraitCollection {
class func wCompact_hCompact() -> UITraitCollection {
let wSizeClass = UIUserInterfaceSizeClass.Compact
let hSizeClass = UIUserInterfaceSizeClass.Compact
return generateTrait([wSizeClass,hSizeClass])
}
class func wCompact_hRegular() -> UITraitCollection {
let wSizeClass = UIUserInterfaceSizeClass.Compact
let hSizeClass = UIUserInterfaceSizeClass.Regular
return generateTrait([wSizeClass,hSizeClass])
}
class func wCompact_hAny() -> UITraitCollection {
let wSizeClass = UIUserInterfaceSizeClass.Compact
let hSizeClass = UIUserInterfaceSizeClass.Unspecified
return generateTrait([wSizeClass,hSizeClass])
}
class func wRegular_hRegular() -> UITraitCollection {
let wSizeClass = UIUserInterfaceSizeClass.Regular
let hSizeClass = UIUserInterfaceSizeClass.Regular
return generateTrait([wSizeClass,hSizeClass])
}
class func wRegular_hCompact() -> UITraitCollection {
let wSizeClass = UIUserInterfaceSizeClass.Regular
let hSizeClass = UIUserInterfaceSizeClass.Compact
return generateTrait([wSizeClass,hSizeClass])
}
class func wRegular_hAny() -> UITraitCollection {
let wSizeClass = UIUserInterfaceSizeClass.Regular
let hSizeClass = UIUserInterfaceSizeClass.Unspecified
return generateTrait([wSizeClass,hSizeClass])
}
class func wAny_hAny() -> UITraitCollection {
let wSizeClass = UIUserInterfaceSizeClass.Unspecified
let hSizeClass = UIUserInterfaceSizeClass.Unspecified
return generateTrait([wSizeClass,hSizeClass])
}
private class func generateTrait(whSizeClass : [UIUserInterfaceSizeClass]) -> UITraitCollection {
let wCollection = UITraitCollection(horizontalSizeClass: whSizeClass[0])
let hCollection = UITraitCollection(verticalSizeClass: whSizeClass[1])
return UITraitCollection(traitsFromCollections: [wCollection,hCollection])
}
}
@available(iOS 9.0, *)
enum LayoutTrait : Int {
case FullScreen = 0
case HorizontalRegular //横屏 2/3
case HorizontalCenter //横屏 1/2
case HorizontalCompact //横屏 1/3
case VerticalRegular //竖屏 2/3
case VerticalCompact //竖屏 1/3
case None
}
@available(iOS 9.0, *)
func currentLayoutTrait() -> LayoutTrait {
if UIDevice.currentDevice().userInterfaceIdiom != UIUserInterfaceIdiom.Pad {
return .None
}
let windowSize = UIApplication.sharedApplication().keyWindow?.bounds.size ?? CGSizeZero
if CGSizeEqualToSize(windowSize, CGSizeZero) {
//如果windowSize 等于 CGSizeZero 应该 keyWindow 初始化时 没有直接使用 init 初始化导致
return .None
}
let screenSize = UIScreen.mainScreen().bounds.size
if CGSizeEqualToSize(screenSize, CGSizeMake(1366, 1024)) || CGSizeEqualToSize(screenSize, CGSizeMake(1024, 1366)) {
let sizeTuple = (windowSize.width,windowSize.height)
switch sizeTuple {
case (981,1024):
return .HorizontalRegular
case (678,1024):
return .HorizontalCenter
case (375,1024):
return .HorizontalCompact
case (375, 1366):
return .VerticalCompact
case (639, 1366):
return .VerticalRegular
case (1366, 1024):
return .FullScreen
case (1024, 1366):
return .FullScreen
default:
return .None
}
}else {
let sizeTuple = (windowSize.width,windowSize.height)
switch sizeTuple {
case (694,768):
return .HorizontalRegular
case (507,768):
return .HorizontalCenter
case (320,768):
return .HorizontalCompact
case (320,1024):
return .VerticalCompact
case (438,1024):
return .VerticalRegular
case (1024, 768):
return .FullScreen
case (768, 1024):
return .FullScreen
default:
return .None
}
}
}