-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathUIView+Positioning.swift
More file actions
executable file
·197 lines (170 loc) · 6.01 KB
/
UIView+Positioning.swift
File metadata and controls
executable file
·197 lines (170 loc) · 6.01 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//
// UIView+Positioning.swift
//
// Created by Shai Mishali on 1/19/15.
// Copyright (c) 2015 Shai Mishali. All rights reserved.
//
import UIKit
public extension UIView {
// MARK: - Basic Properties
/// X Axis value of UIView.
var x: CGFloat {
set { self.frame = CGRect(x: _pixelIntegral(newValue),
y: self.y,
width: self.width,
height: self.height)
}
get { return self.frame.origin.x }
}
/// Y Axis value of UIView.
var y: CGFloat {
set { self.frame = CGRect(x: self.x,
y: _pixelIntegral(newValue),
width: self.width,
height: self.height)
}
get { return self.frame.origin.y }
}
/// Width of view.
var width: CGFloat {
set { self.frame = CGRect(x: self.x,
y: self.y,
width: _pixelIntegral(newValue),
height: self.height)
}
get { return self.frame.size.width }
}
/// Height of view.
var height: CGFloat {
set { self.frame = CGRect(x: self.x,
y: self.y,
width: self.width,
height: _pixelIntegral(newValue))
}
get { return self.frame.size.height }
}
// MARK: - Origin and Size
/// View's Origin point.
var origin: CGPoint {
set { self.frame = CGRect(x: _pixelIntegral(newValue.x),
y: _pixelIntegral(newValue.y),
width: self.width,
height: self.height)
}
get { return self.frame.origin }
}
/// View's size.
var size: CGSize {
set { self.frame = CGRect(x: self.x,
y: self.y,
width: _pixelIntegral(newValue.width),
height: _pixelIntegral(newValue.height))
}
get { return self.frame.size }
}
// MARK: - Extra Properties
/// View's right side (x + width).
var right: CGFloat {
set { self.x = newValue - self.width }
get { return self.x + self.width }
}
/// View's bottom (y + height).
var bottom: CGFloat {
set { self.y = newValue - self.height }
get { return self.y + self.height }
}
/// View's top (y).
var top: CGFloat {
set { self.y = newValue }
get { return self.y }
}
/// View's left side (x).
var left: CGFloat {
set { self.x = newValue }
get { return self.x }
}
/// View's center X value (center.x).
var centerX: CGFloat {
set { self.center = CGPoint(x: newValue, y: self.centerY) }
get { return self.center.x }
}
/// View's center Y value (center.y).
var centerY: CGFloat {
set { self.center = CGPoint(x: self.centerX, y: newValue) }
get { return self.center.y }
}
/// Last subview on X Axis.
var lastSubviewOnX: UIView? {
return self.subviews.reduce(UIView(frame: .zero)) {
return $1.x > $0.x ? $1 : $0
}
}
/// Last subview on Y Axis.
var lastSubviewOnY: UIView? {
return self.subviews.reduce(UIView(frame: .zero)) {
return $1.y > $0.y ? $1 : $0
}
}
// MARK: - Bounds Methods
/// X value of bounds (bounds.origin.x).
var boundsX: CGFloat {
set { self.bounds = CGRect(x: _pixelIntegral(newValue),
y: self.boundsY,
width: self.boundsWidth,
height: self.boundsHeight)
}
get { return self.bounds.origin.x }
}
/// Y value of bounds (bounds.origin.y).
var boundsY: CGFloat {
set { self.frame = CGRect(x: self.boundsX,
y: _pixelIntegral(newValue),
width: self.boundsWidth,
height: self.boundsHeight)
}
get { return self.bounds.origin.y }
}
/// Width of bounds (bounds.size.width).
var boundsWidth: CGFloat {
set { self.frame = CGRect(x: self.boundsX,
y: self.boundsY,
width: _pixelIntegral(newValue),
height: self.boundsHeight)
}
get { return self.bounds.size.width }
}
/// Height of bounds (bounds.size.height).
var boundsHeight: CGFloat {
set { self.frame = CGRect(x: self.boundsX,
y: self.boundsY,
width: self.boundsWidth,
height: _pixelIntegral(newValue))
}
get { return self.bounds.size.height }
}
// MARK: - Useful Methods
/// Center view to it's parent view.
// func centerToParent() {
// guard let superview = self.superview else { return }
//
// switch UIApplication.shared.statusBarOrientation {
// case .landscapeLeft, .landscapeRight:
// self.origin = CGPoint(x: (superview.height / 2) - (self.width / 2),
// y: (superview.width / 2) - (self.height / 2))
// case .portrait, .portraitUpsideDown:
// self.origin = CGPoint(x: (superview.width / 2) - (self.width / 2),
// y: (superview.height / 2) - (self.height / 2))
// case .unknown:
// return
// }
// }
// MARK: - Private Methods
fileprivate func _pixelIntegral(_ pointValue: CGFloat) -> CGFloat {
#if os(iOS)
let scale = UIScreen.main.scale
return (round(pointValue * scale) / scale)
#else
return pointValue
#endif
}
}