forked from freak4pc/UIView-Positioning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUIView+Positioning.swift
168 lines (138 loc) · 5.15 KB
/
UIView+Positioning.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
//
// UIView+Positioning.swift
//
// Created by Shai Mishali on 1/19/15.
// Copyright (c) 2015 Shai Mishali. All rights reserved.
// Updated to Swift 3.0 by Muhammad Assar <[email protected]> on 12/1/2016
import UIKit
extension UIView {
// MARK: - Basic Properties
/// X Axis value of UIView.
var x: CGFloat {
set { self.frame = CGRect(x: 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: 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: 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: newValue) }
get { return self.frame.size.height }
}
// MARK: - Origin and Size
/// View's origin point.
var origin: CGPoint {
set { self.frame = CGRect(x: newValue.x,y: 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: newValue.width,height: 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? {
get {
var outView:UIView = self.subviews[0] as UIView
for v in self.subviews as [UIView] {
if(v.x > outView.x){ outView = v }
}
return outView
}
}
/// Last subview on Y Axis.
var lastSubviewOnY:UIView? {
get {
var outView:UIView = self.subviews[0] as UIView
for v in self.subviews as [UIView] {
if(v.y > outView.y){ outView = v }
}
return outView
}
}
// MARK: - Bounds Methods
/// X value of bounds (bounds.origin.x).
var boundsX:CGFloat {
set{ self.bounds = CGRect(x: 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: 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: 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: newValue) }
get { return self.bounds.size.height }
}
// MARK: - Useful Methods
/// Center view to it's parent view.
func centerToParent(){
if(self.superview != nil){
switch(UIApplication.shared.statusBarOrientation){
case .landscapeLeft:
fallthrough
case .landscapeRight:
self.origin = CGPoint(x: (self.superview!.height / 2) - (self.width / 2),
y: (self.superview!.width / 2) - (self.height / 2))
case .portrait:
fallthrough
case .portraitUpsideDown:
self.origin = CGPoint(x: (self.superview!.width / 2) - (self.width / 2),
y: (self.superview!.height / 2) - (self.height / 2))
case .unknown:
return
}
}
}
// MARK: - Private Methods
private func _pixelIntegral(pointValue:CGFloat) -> CGFloat{
let scale = UIScreen.main.scale
return (round(pointValue * scale) / scale)
}
}