-
Notifications
You must be signed in to change notification settings - Fork 2
/
UIView+position.m
116 lines (89 loc) · 3.04 KB
/
UIView+position.m
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
//
// UIView+position.m
//
// Apache license
//
// Created by Tyler Neylon on 3/19/10 (http://bynomial.com/blog/?p=24)
// Copyleft 2010 Bynomial.
//
#import "UIView+position.h"
@implementation UIView (position)
- (CGPoint)origin {
return self.frame.origin;
}
- (void)setOrigin:(CGPoint)newOrigin {
self.frame = CGRectMake(newOrigin.x, newOrigin.y, self.frame.size.width, self.frame.size.height);
}
- (CGSize)size {
return self.frame.size;
}
- (void)setSize:(CGSize)newSize {
self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y,
newSize.width, newSize.height);
}
- (CGFloat)rx {
return self.frame.origin.x;
}
- (void)setRx:(CGFloat)newX {
self.frame = CGRectMake(newX, self.frame.origin.y,
self.frame.size.width, self.frame.size.height);
}
- (CGFloat)ry {
return self.frame.origin.y;
}
- (void)setRy:(CGFloat)newY {
self.frame = CGRectMake(self.frame.origin.x, newY,
self.frame.size.width, self.frame.size.height);
}
- (CGFloat)right {
return self.frame.origin.x + self.frame.size.width;
}
- (void)setRight:(CGFloat)newRight {
self.frame = CGRectMake(newRight - self.frame.size.width, self.frame.origin.y,
self.frame.size.width, self.frame.size.height);
}
- (CGFloat)bottom {
return self.frame.origin.y + self.frame.size.height;
}
- (void)setBottom:(CGFloat)newBottom {
self.frame = CGRectMake(self.frame.origin.x, newBottom - self.frame.size.height,
self.frame.size.width, self.frame.size.height);
}
- (CGFloat)width {
return self.frame.size.width;
}
- (void)setWidth:(CGFloat)newWidth {
self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y,
newWidth, self.frame.size.height);
}
- (CGFloat)height {
return self.frame.size.height;
}
- (void)setHeight:(CGFloat)newHeight {
self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y,
self.frame.size.width, newHeight);
}
- (void)addCenteredSubview:(UIView *)subview {
subview.rx = (int)((self.bounds.size.width - subview.frame.size.width) / 2);
subview.ry = (int)((self.bounds.size.height - subview.frame.size.height) / 2);
[self addSubview:subview];
}
- (void)moveToCenterOfSuperview {
if (!self.superview)
NSLog(@"Trying to move view inside superview before attaching. Expect weird stuff.");
self.rx = (int)((self.superview.bounds.size.width - self.frame.size.width) / 2);
self.ry = (int)((self.superview.bounds.size.height - self.frame.size.height) / 2);
}
- (void)centerVerticallyInSuperview
{
if (!self.superview)
NSLog(@"Trying to move view inside superview before attaching. Expect weird stuff.");
self.ry = (int)((self.superview.bounds.size.height - self.frame.size.height) / 2);
}
- (void)centerHorizontallyInSuperview
{
if (!self.superview)
NSLog(@"Trying to move view inside superview before attaching. Expect weird stuff.");
self.rx = (int)((self.superview.bounds.size.width - self.frame.size.width) / 2);
}
@end