想要快速布局,又不想用第三方布局的同胞们可以自己写快速布局的方法。之前为了布局简单对UIView进行了一些扩展,很多人都用到过,在UIView类别中我们可以设置UIView x,y,width,height声明,自己实现设置获取的方法,用着确实方便了许多。在看之前代码时发现在设置单个frame的一般属性这种方法着实方便,但是当相对于其他view进行设置对齐就略显薄弱。
为了偷懒,还要看着简洁,就想着自己写一下方法,这次时间有限,写了几个简单常用的方法,maxX获取view最大X值,maxY获取view最大Y值,centerxEqualToView设置于目标view的centerx对齐,centeryEqualToView设置于目标view的centery对齐等方法。
UIView类目git地址:
https://github.com/MisterZhouZhou/ZWUIViewExtension
对齐原理很简单就是利用block传值。
version1:
这种对齐方式只能解决非子父关系的view布局要求
在UIView类目.h中声明属性
/** x + width */
@property(nonatomic,assign) CGFloat maxX;
/** y + height */
@property(nonatomic,assign) CGFloat maxY;
/** centerX equal to View‘s centerX
* centerxEqualToView(superview/view)
*/
-(void(^)(UIView *view))centerxEqualToView;
/** centerY equal to View‘s centerY
* centeryEqualToView(superview/view)
*/
-(void(^)(UIView *view))centeryEqualToView;
/** center equal to View‘s center
* centerEqualToView(superview/view)
*/
-(void(^)(UIView *view))centerEqualToView;
/** left equal to View‘s left
* leftEqualToView(superview/view)
*/
-(void(^)(UIView *view))leftEqualToView;
/** right equal to View‘s right
* rightEqualToView(superview/view)
*/
-(void(^)(UIView *view))rightEqualToView;
/** top equal to View‘s top
* topEqualToView(superview/view)
*/
-(void(^)(UIView *view))topEqualToView;
/** bottom equal to View‘s bottom
* bottomEqualToView(superview/view)
*/
-(void(^)(UIView *view))bottomEqualToView;
在UIView类目.m中简单实现:
-(CGFloat)maxX
{
return self.frame.origin.x + self.frame.size.width;
}
-(CGFloat)maxY
{
return self.frame.origin.y + self.frame.size.height;
}
#pragma mark - equalToView
-(void(^)(UIView *view))centerxEqualToView{
@WeakObj(self);
return ^(UIView *view){
@StrongObj(self);
CGFloat centerx = view.center.x;
self.center = CGPointMake(centerx, self.center.y);
};
}
-(void(^)(UIView *view))centeryEqualToView{
@WeakObj(self);
return ^(UIView *view){
@StrongObj(self);
self.center = CGPointMake(self.center.x, view.center.y);
};
}
-(void(^)(UIView *view))centerEqualToView{
@WeakObj(self);
return ^(UIView *view){
@StrongObj(self);
self.center = view.center;
};
}
-(void(^)(UIView *view))leftEqualToView{
@WeakObj(self);
return ^(UIView *view){
@StrongObj(self);
self.x = view.x;
};
}
-(void(^)(UIView *view))rightEqualToView{
@WeakObj(self);
return ^(UIView *view){
@StrongObj(self);
self.maxX = view.maxX;
};
}
-(void(^)(UIView *view))topEqualToView{
@WeakObj(self);
return ^(UIView *view){
@StrongObj(self);
self.y = view.y;
};
}
-(void(^)(UIView *view))bottomEqualToView{
@WeakObj(self);
return ^(UIView *view){
@StrongObj(self);
self.maxY = view.maxY;
};
}
偷懒后,就可以这样调用了。
loginBgView.y = loginView.maxY = 10;
loginBgView.centerxEqualToView(loginView);
//loginBgView.centeryEqualToView(loginView);
//loginBgView.centerEqualToView(loginView);
version2:
兼容含子父关系的view布局要求,布局需要先添加view再进行布局
于是做了以下变动
#pragma mark - equalToView
-(void(^)(UIView *view))centerxEqualToView{
@WeakObj(self);
return ^(UIView *view){
@StrongObj(self);
//如果是子父关系
if ([self.superview isKindOfClass:[view class]]) {
self.center = CGPointMake(view.center.x - view.x, self.center.y);
}else{
self.center = CGPointMake(view.center.x, self.center.y);
}
};
}
-(void(^)(UIView *view))centeryEqualToView{
@WeakObj(self);
return ^(UIView *view){
@StrongObj(self);
//如果是子父关系
if ([self.superview isKindOfClass:[view class]]) {
self.center = CGPointMake(self.center.x, view.center.y - view.y);
}else{
self.center = CGPointMake(self.center.x, view.center.y);
}
};
}
-(void(^)(UIView *view))centerEqualToView{
@WeakObj(self);
return ^(UIView *view){
@StrongObj(self);
//如果是子父关系
if ([self.superview isKindOfClass:[view class]]) {
self.center = CGPointMake(view.center.x - view.x , view.center.y - view.y);
}
else{
self.center = view.center;
}
};
}
-(void(^)(UIView *view))leftEqualToView{
@WeakObj(self);
return ^(UIView *view){
@StrongObj(self);
//如果是子父关系
if ([self.superview isKindOfClass:[view class]]) {
self.center = CGPointMake(view.center.x - view.x,self.center.y);
self.x = self.centerX - view.width/2;
}else{
self.x = view.x;
}
};
}
-(void(^)(UIView *view))rightEqualToView{
@WeakObj(self);
return ^(UIView *view){
@StrongObj(self);
//如果是子父关系
if ([self.superview isKindOfClass:[view class]]) {
self.center = CGPointMake(view.center.x - view.x,self.center.y);
self.maxX = self.centerX + view.width/2;
}else{
self.maxX = view.maxX;
}
};
}
-(void(^)(UIView *view))topEqualToView{
@WeakObj(self);
return ^(UIView *view){
@StrongObj(self);
//如果是子父关系
if ([self.superview isKindOfClass:[view class]]) {
self.center = CGPointMake(self.center.x,view.center.y - view.y);
self.y = self.centerY - view.height/2;
}else{
self.y = view.y;
}
};
}
-(void(^)(UIView *view))bottomEqualToView{
@WeakObj(self);
return ^(UIView *view){
@StrongObj(self);
//如果是子父关系
if ([self.superview isKindOfClass:[view class]]) {
self.center = CGPointMake(self.center.x,view.center.y - view.y);
self.maxY = self.centerY + view.height/2;
}else{
self.maxY = view.maxY;
}
};
}