/
UIView+MTUIAdditions.m
160 lines (122 loc) · 4.53 KB
/
UIView+MTUIAdditions.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
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
//
// UIView+MTAdditions.m
// myell0w-helper
//
// Created by Matthias Tretter on 26.09.10.
// Copyright 2010 YellowSoft. All rights reserved.
//
#import "UIView+MTUIAdditions.h"
#include "UIView+Sizes.h"
#import <QuartzCore/QuartzCore.h>
@implementation UIView (MTUIAdditions)
//===========================================================
#pragma mark -
#pragma mark Properties for retreiving/changing of Frame Coordinates
//===========================================================
- (void)setFrameWidth:(CGFloat)width {
self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, width, self.frame.size.height);
}
- (void)setFrameHeight:(CGFloat)height {
self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, height);
}
- (void)setFrameTop:(CGFloat)top {
self.frame = CGRectMake(self.frame.origin.x, top, self.frame.size.width, self.frame.size.height);
}
- (void)setFrameLeft:(CGFloat)left {
self.frame = CGRectMake(left, self.frame.origin.y, self.frame.size.width, self.frame.size.height);
}
- (void)setFrameBottom:(CGFloat)bottom {
self.frame = CGRectMake(self.frame.origin.x,bottom - self.frame.size.height, self.frame.size.width, self.frame.size.height);
}
- (void)setFrameRight:(CGFloat)right {
self.frame = CGRectMake(right - self.frame.size.width,self.frame.origin.y, self.frame.size.width, self.frame.size.height);
}
- (CGFloat)frameHeight {
return self.frame.size.height;
}
- (CGFloat)frameWidth {
return self.frame.size.width;
}
- (CGFloat)frameTop {
return self.frame.origin.y;
}
- (CGFloat)frameLeft {
return self.frame.origin.x;
}
- (CGFloat)frameBottom {
return self.frame.origin.y + self.frame.size.height;
}
- (CGFloat)frameRight {
return self.frame.origin.x + self.frame.size.width;
}
//===========================================================
#pragma mark -
#pragma mark Position Views
//===========================================================
- (void)positionUnderView:(UIView *)view {
[self positionUnderView:view padding:0];
}
- (void)positionUnderView:(UIView *)view padding:(CGFloat)padding {
[self positionUnderView:view padding:padding alignment:MTUIViewAlignmentUnchanged];
}
- (void)positionUnderView:(UIView *)view alignment:(MTUIViewAlignment)alignment {
[self positionUnderView:view padding:0 alignment:alignment];
}
- (void)positionUnderView:(UIView *)view padding:(CGFloat)padding alignment:(MTUIViewAlignment)alignment {
self.frameTop = view.frameBottom + padding;
switch (alignment) {
case MTUIViewAlignmentUnchanged:
// do nothing
break;
case MTUIViewAlignmentLeftAligned:
self.frameLeft = view.frameLeft;
break;
case MTUIViewAlignmentRightAligned:
self.frameRight = view.frameRight;
break;
case MTUIViewAlignmentCenter:
self.centerX = view.centerX;
break;
}
}
- (void)addCenteredSubview:(UIView *)subview {
subview.frameLeft = (CGFloat)((self.bounds.size.width - subview.frameWidth) / 2);
subview.frameTop = (CGFloat)((self.bounds.size.height - subview.frameHeight) / 2);
[self addSubview:subview];
}
- (void)moveToCenterOfSuperview {
self.frameLeft = (CGFloat)((self.superview.bounds.size.width - self.frameWidth) / 2);
self.frameTop = (CGFloat)((self.superview.bounds.size.height - self.frameHeight) / 2);
}
//===========================================================
#pragma mark -
#pragma mark Rounded Corners
//===========================================================
- (void)setCornerRadius:(CGFloat)cornerRadius borderWidth:(CGFloat)borderWidth borderColor:(UIColor *)borderColor {
CALayer *l = [self layer];
l.masksToBounds = YES;
l.cornerRadius = cornerRadius;
l.borderWidth = borderWidth;
l.borderColor = [borderColor CGColor];
}
//===========================================================
#pragma mark -
#pragma mark Shadows
//===========================================================
- (void)setShadowOffset:(CGSize)offset radius:(CGFloat)radius opacity:(CGFloat)opacity {
self.layer.masksToBounds = NO;
self.layer.shadowOffset = offset;
self.layer.shadowRadius = radius;
self.layer.shadowOpacity = opacity;
}
//===========================================================
#pragma mark -
#pragma mark Gradient Background
//===========================================================
- (void)setGradientBackgroundWithStartColor:(UIColor *)startColor endColor:(UIColor *)endColor {
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = self.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[startColor CGColor], (id)[endColor CGColor], nil];
[self.layer insertSublayer:gradient atIndex:0];
}
@end