-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUIView+Alignment.m
62 lines (44 loc) · 1.35 KB
/
UIView+Alignment.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
//
// UIView+Alignment.m
// UIView+Alignment
//
// Created by Dan Hassin on 4/6/10.
// Copyright 2010 InContext. All rights reserved.
//
#import "UIView+Alignment.h"
@implementation UIView (alignment)
- (void) alignTo:(UIViewAlignment)a margins:(UIEdgeInsets)e
{
//default is to align to superview
[self alignTo:a ofRect:self.superview.bounds margins:e];
}
- (void) alignTo:(UIViewAlignment)a
{
[self alignTo:a margins:UIEdgeInsetsZero];
}
- (void) alignTo:(UIViewAlignment)a ofRect:(CGRect)r margins:(UIEdgeInsets)e
{
CGRect rect = self.frame;
if (a & UIViewAlignmentCenterHorizontal)
rect.origin.x = r.origin.x + (r.size.width-rect.size.width)/2.0;
if (a & UIViewAlignmentCenterVertical)
rect.origin.y = r.origin.y + (r.size.height-rect.size.height)/2.0;
if (a & UIViewAlignmentTop)
rect.origin.y = r.origin.y;
if (a & UIViewAlignmentBottom)
rect.origin.y = r.origin.y + r.size.height-self.frame.size.height;
if (a & UIViewAlignmentLeft)
rect.origin.x = r.origin.x;
if (a & UIViewAlignmentRight)
rect.origin.x = r.origin.x + r.size.width-self.frame.size.width;
rect.origin.x += e.left;
rect.origin.x -= e.right;
rect.origin.y += e.top;
rect.origin.y -= e.bottom;
self.frame = CGRectIntegral(rect);
}
- (void) alignTo:(UIViewAlignment)a ofRect:(CGRect)rect
{
[self alignTo:a ofRect:rect margins:UIEdgeInsetsZero];
}
@end