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
#import <Foundation/Foundation.h>

typedef enum
{
   AlignmentCenter = 0,
   AlignmentTop,
   AlignmentBottom,
   AlignmentLeft,
   AlignmentRight
} PlaceAlignmentType;

@interface PositionTools : NSObject

// 针对默认为竖屏的有效
+ (void)placeView:(UIView*)subview atTheCenterOfTheView:(UIView*)container;

+ (void)placeView:(UIView*)subview atTheLeftMiddleOfTheView:(UIView*)container offset:(float)offset;
+ (void)placeView:(UIView*)subview atTheRightMiddleOfTheView:(UIView*)container offset:(float)offset;
+ (void)placeView:(UIView*)subview atTheTopMiddleOfTheView:(UIView*)container offset:(float)offset;
+ (void)placeView:(UIView*)subview atTheBottomMiddleOfTheView:(UIView*)container offset:(float)offset;

+ (void)placeView:(UIView*)subview atTheLeftTopOfTheView:(UIView*)container offset:(CGSize)offset;
+ (void)placeView:(UIView*)subview atTheRightTopOfTheView:(UIView*)container offset:(CGSize)offset;
+ (void)placeView:(UIView*)subview atTheLeftBottomOfTheView:(UIView*)container offset:(CGSize)offset;
+ (void)placeView:(UIView*)subview atTheRightBottomOfTheView:(UIView*)container offset:(CGSize)offset;

+ (void)placeView:(UIView *)sourceView toTheRightOfTheView:(UIView*)targetView span:(float)span;
+ (void)placeView:(UIView *)sourceView toTheLeftOfTheView:(UIView*)targetView span:(float)span;
+ (void)placeView:(UIView *)sourceView aboveTheView:(UIView*)targetView span:(float)span;
+ (void)placeView:(UIView *)sourceView belowTheView:(UIView*)targetView span:(float)span;

+ (void)placeView:(UIView *)sourceView toTheRightOfTheView:(UIView*)targetView span:(float)span alignmentType:(PlaceAlignmentType)alignmentType;
+ (void)placeView:(UIView *)sourceView toTheLeftOfTheView:(UIView*)targetView span:(float)span alignmentType:(PlaceAlignmentType)alignmentType;
+ (void)placeView:(UIView *)sourceView aboveTheView:(UIView*)targetView span:(float)span alignmentType:(PlaceAlignmentType)alignmentType;
+ (void)placeView:(UIView *)sourceView belowTheView:(UIView*)targetView span:(float)span alignmentType:(PlaceAlignmentType)alignmentType;

// 用来处理横屏的情况下,在屏幕没有旋转到横屏位置之前的情况
+ (void)placeView:(UIView*)subview atTheCenterOfTheView:(UIView*)container needAlter:(BOOL)needAlter;

+ (void)placeView:(UIView*)subview atTheLeftMiddleOfTheView:(UIView*)container offset:(float)offset needAlter:(BOOL)needAlter;
+ (void)placeView:(UIView*)subview atTheRightMiddleOfTheView:(UIView*)container offset:(float)offset needAlter:(BOOL)needAlter;
+ (void)placeView:(UIView*)subview atTheTopMiddleOfTheView:(UIView*)container offset:(float)offset needAlter:(BOOL)needAlter;
+ (void)placeView:(UIView*)subview atTheBottomMiddleOfTheView:(UIView*)container offset:(float)offset needAlter:(BOOL)needAlter;

+ (void)placeView:(UIView*)subview atTheRightTopOfTheView:(UIView*)container offset:(CGSize)offset needAlter:(BOOL)needAlter;
+ (void)placeView:(UIView*)subview atTheLeftBottomOfTheView:(UIView*)container offset:(CGSize)offset needAlter:(BOOL)needAlter;
+ (void)placeView:(UIView*)subview atTheRightBottomOfTheView:(UIView*)container offset:(CGSize)offset needAlter:(BOOL)needAlter;

@end

实现

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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#import "PositionTools.h"

@implementation PositionTools

+ (void)placeView:(UIView*)subview atTheCenterOfTheView:(UIView*)container
{
   return [self placeView:subview atTheCenterOfTheView:container needAlter:NO];
}

+ (void)placeView:(UIView*)subview atTheLeftMiddleOfTheView:(UIView*)container offset:(float)offset
{
   return [self placeView:subview atTheLeftMiddleOfTheView:container offset:offset needAlter:NO];
}

+ (void)placeView:(UIView*)subview atTheRightMiddleOfTheView:(UIView*)container offset:(float)offset
{
   return [self placeView:subview atTheRightMiddleOfTheView:container offset:offset needAlter:NO];
}

+ (void)placeView:(UIView*)subview atTheTopMiddleOfTheView:(UIView*)container offset:(float)offset
{
   return [self placeView:subview atTheTopMiddleOfTheView:container offset:offset needAlter:NO];
}

+ (void)placeView:(UIView*)subview atTheBottomMiddleOfTheView:(UIView*)container offset:(float)offset
{
   return [self placeView:subview atTheBottomMiddleOfTheView:container offset:offset needAlter:NO];
}

+ (void)placeView:(UIView*)subview atTheRightTopOfTheView:(UIView*)container offset:(CGSize)offset
{
   return [self placeView:subview atTheRightTopOfTheView:container offset:offset needAlter:NO];
}

+ (void)placeView:(UIView*)subview atTheLeftBottomOfTheView:(UIView*)container offset:(CGSize)offset
{
   return [self placeView:subview atTheLeftBottomOfTheView:container offset:offset needAlter:NO];
}

+ (void)placeView:(UIView*)subview atTheRightBottomOfTheView:(UIView*)container offset:(CGSize)offset
{
   return [self placeView:subview atTheRightBottomOfTheView:container offset:offset needAlter:NO];
}

+ (void)placeView:(UIView*)subview atTheCenterOfTheView:(UIView*)container needAlter:(BOOL)needAlter
{
   [container addSubview:subview];
   
   if (needAlter) {
       subview.center = CGPointMake(container.bounds.size.height/2, container.bounds.size.width/2);
   } else {
       subview.center = CGPointMake(container.bounds.size.width/2, container.bounds.size.height/2);
   }
}

+ (void)placeView:(UIView*)subview atTheLeftMiddleOfTheView:(UIView*)container offset:(float)offset needAlter:(BOOL)needAlter
{
   [container addSubview:subview];
   
   if (needAlter) {
       subview.center = CGPointMake(offset + subview.frame.size.width/2, container.bounds.size.width/2);
   } else {
       subview.center = CGPointMake(offset + subview.frame.size.width/2, container.bounds.size.height/2);
   }
}

+ (void)placeView:(UIView*)subview atTheRightMiddleOfTheView:(UIView*)container offset:(float)offset needAlter:(BOOL)needAlter
{
   [container addSubview:subview];
   
   if (needAlter) {
       subview.center = CGPointMake(container.bounds.size.height - (offset + subview.frame.size.width/2), container.bounds.size.width/2);
   } else {
       subview.center = CGPointMake(container.bounds.size.width - (offset + subview.frame.size.width/2), container.bounds.size.height/2);
   }
}

+ (void)placeView:(UIView*)subview atTheTopMiddleOfTheView:(UIView*)container offset:(float)offset needAlter:(BOOL)needAlter
{
   [container addSubview:subview];
   
   if (needAlter) {
       subview.center = CGPointMake(container.bounds.size.height/2, offset + subview.frame.size.height/2);
   } else {
       subview.center = CGPointMake(container.bounds.size.width/2, offset + subview.frame.size.height/2);
   }
}

+ (void)placeView:(UIView*)subview atTheBottomMiddleOfTheView:(UIView*)container offset:(float)offset needAlter:(BOOL)needAlter
{
   [container addSubview:subview];
   
   if (needAlter) {
       subview.center = CGPointMake(container.bounds.size.height/2,  container.bounds.size.width - (offset + subview.frame.size.height/2));
   } else {
       subview.center = CGPointMake(container.bounds.size.width/2,  container.bounds.size.height - (offset + subview.frame.size.height/2));
   }
}

+ (void)placeView:(UIView*)subview atTheLeftTopOfTheView:(UIView*)container offset:(CGSize)offset
{
   [container addSubview:subview];
   
   subview.center = CGPointMake(offset.width + subview.frame.size.width/2,
                                offset.height + subview.frame.size.height/2);
}

+ (void)placeView:(UIView*)subview atTheRightTopOfTheView:(UIView*)container offset:(CGSize)offset needAlter:(BOOL)needAlter
{
   [container addSubview:subview];
   
   if (needAlter) {
       subview.center = CGPointMake(container.bounds.size.height - (offset.width + subview.frame.size.width/2),
                                    offset.height + subview.frame.size.height/2);
   } else {
       subview.center = CGPointMake(container.bounds.size.width - (offset.width + subview.frame.size.width/2),
                                    offset.height + subview.frame.size.height/2);
   }
}

+ (void)placeView:(UIView*)subview atTheLeftBottomOfTheView:(UIView*)container offset:(CGSize)offset needAlter:(BOOL)needAlter
{
   [container addSubview:subview];
   
   if (needAlter) {
       subview.center = CGPointMake(offset.width + subview.frame.size.width/2,
                                    container.bounds.size.width - (offset.height + subview.frame.size.height/2));
   } else {
       subview.center = CGPointMake(offset.width + subview.frame.size.width/2,
                                    container.bounds.size.height - (offset.height + subview.frame.size.height/2));
   }
}

+ (void)placeView:(UIView*)subview atTheRightBottomOfTheView:(UIView*)container offset:(CGSize)offset needAlter:(BOOL)needAlter
{
   [container addSubview:subview];
   
   if (needAlter) {
       subview.center = CGPointMake(container.bounds.size.height - (offset.width + subview.frame.size.width/2),
                                    container.bounds.size.width - (offset.height + subview.frame.size.height/2));
   } else {
       subview.center = CGPointMake(container.bounds.size.width - (offset.width + subview.frame.size.width/2),
                                    container.bounds.size.height - (offset.height + subview.frame.size.height/2));
   }
}

+ (void)placeView:(UIView *)sourceView toTheRightOfTheView:(UIView*)targetView span:(float)span
{
   [self placeView:sourceView toTheRightOfTheView:targetView span:span alignmentType:AlignmentCenter];
}

+ (void)placeView:(UIView *)sourceView toTheLeftOfTheView:(UIView*)targetView span:(float)span
{
   [self placeView:sourceView toTheLeftOfTheView:targetView span:span alignmentType:AlignmentCenter];
}

+ (void)placeView:(UIView *)sourceView aboveTheView:(UIView*)targetView span:(float)span
{
   [self placeView:sourceView aboveTheView:targetView span:span alignmentType:AlignmentCenter];
}

+ (void)placeView:(UIView *)sourceView belowTheView:(UIView*)targetView span:(float)span
{
   [self placeView:sourceView belowTheView:targetView span:span alignmentType:AlignmentCenter];
}

+ (void)placeView:(UIView*)sourceView toTheRightOfTheView:(UIView*)targetView span:(float)span alignmentType:(PlaceAlignmentType)alignmentType
{
   [[targetView superview] addSubview:sourceView];
   sourceView.center = CGPointMake(targetView.frame.origin.x + targetView.frame.size.width + (span + sourceView.frame.size.width/2),
                                   targetView.center.y);
   [self alignView:sourceView targetView:targetView alignmentType:alignmentType];
}

+ (void)placeView:(UIView*)sourceView toTheLeftOfTheView:(UIView*)targetView span:(float)span alignmentType:(PlaceAlignmentType)alignmentType
{
   [[targetView superview] addSubview:sourceView];
   sourceView.center = CGPointMake(targetView.frame.origin.x - (span + sourceView.frame.size.width/2),
                                   targetView.center.y);
   [self alignView:sourceView targetView:targetView alignmentType:alignmentType];
}

+ (void)placeView:(UIView*)sourceView aboveTheView:(UIView*)targetView span:(float)span alignmentType:(PlaceAlignmentType)alignmentType
{
   [[targetView superview] addSubview:sourceView];
   sourceView.center = CGPointMake(targetView.center.x,
                                   targetView.frame.origin.y - (span + sourceView.frame.size.height/2));
   [self alignView:sourceView targetView:targetView alignmentType:alignmentType];
}

+ (void)placeView:(UIView*)sourceView belowTheView:(UIView*)targetView span:(float)span alignmentType:(PlaceAlignmentType)alignmentType
{
   [[targetView superview] addSubview:sourceView];
   sourceView.center = CGPointMake(targetView.center.x,
                                   targetView.frame.origin.y + targetView.frame.size.height + (span + sourceView.frame.size.height/2));
   [self alignView:sourceView targetView:targetView alignmentType:alignmentType];
}

+ (void)alignView:(UIView*)sourceView targetView:(UIView*)targetView alignmentType:(PlaceAlignmentType)alignmentType
{
   if (AlignmentTop == alignmentType) {
       sourceView.center = CGPointMake(sourceView.center.x,
                                       targetView.frame.origin.y + sourceView.frame.size.height/2);
   } else if (AlignmentBottom == alignmentType) {
       sourceView.center = CGPointMake(sourceView.center.x,
                                       targetView.frame.origin.y + targetView.frame.size.height - sourceView.frame.size.height/2);
   } else if (AlignmentLeft == alignmentType) {
       sourceView.center = CGPointMake(targetView.frame.origin.x + sourceView.frame.size.width/2,
                                       sourceView.center.y);
   } else if (AlignmentRight == alignmentType) {
       sourceView.center = CGPointMake(targetView.frame.origin.x + targetView.frame.size.width - sourceView.frame.size.width/2,
                                       sourceView.center.y);
   }
}
@end