iOS UIView 快速修改 frame,

在iOS开发布局修改 frame 时需要繁琐的代码实现,今天偶尔看到一播客说到快速修改的 frame 的方法,自己动手写了一遍实现代码.

快速实现主要通过 添加类目的方式,对UIView 控件添加了一些直接修改 frame 属性的方法(如:获取高度.宽度,坐标等);具体代码实现如下:

.h文件,声明要用到的属性

 //
// UIView+Layout.h
// Layout
//
// Created by Ager on 15/10/18.
// Copyright © 2015年 Ager. All rights reserved.
// #import <UIKit/UIKit.h> @interface UIView (Layout) //顶,底,左,右
@property (nonatomic , assign)CGFloat top;
@property (nonatomic , assign)CGFloat bottom;
@property (nonatomic , assign)CGFloat left;
@property (nonatomic , assign)CGFloat right; //坐标,x,y
@property (nonatomic , assign)CGFloat x;
@property (nonatomic , assign)CGFloat y;
@property (nonatomic , assign)CGPoint origin; //中心点坐标 centerX,centerY
@property (nonatomic , assign)CGFloat centerX;
@property (nonatomic , assign)CGFloat centerY; //大小 ,宽,高
@property (nonatomic , assign)CGFloat width;
@property (nonatomic , assign)CGFloat height;
@property (nonatomic , assign)CGSize size; @end

.m 文件实现对 属性 的操作.从而实现对 frame 的修改

 //
// UIView+Layout.m
// Layout
//
// Created by Ager on 15/10/18.
// Copyright © 2015年 Ager. All rights reserved.
// #import "UIView+Layout.h" @implementation UIView (Layout) //顶,底,左,右
//top;
- (CGFloat)top{
return self.frame.origin.y;
} - (void)setTop:(CGFloat)top{
CGRect frame = self.frame;
frame.origin.y = top;
self.frame = frame;
} //bottom;
- (CGFloat)bottom{
return CGRectGetMaxY(self.frame);
} - (void)setBottom:(CGFloat)bottom{
CGRect frame = self.frame;
frame.origin.y = [self bottom] - [self height];
self.frame = frame;
} //left;
- (CGFloat)left{
return self.frame.origin.x;
} - (void)setLeft:(CGFloat)left{
CGRect frame = self.frame;
frame.origin.x = left;
self.frame = frame;
} //right;
- (CGFloat)right{
return CGRectGetMaxX(self.frame);
} - (void)setRight:(CGFloat)right{
CGRect frame = self.frame;
frame.origin.x = [self right] - [self width];
self.frame = frame;
} //坐标,x,y
//x;
- (CGFloat)x{
return self.frame.origin.x;
} - (void)setX:(CGFloat)x{
CGRect frame = self.frame;
frame.origin.x = x;
self.frame = frame;
} //y; - (CGFloat)y{
return self.origin.y;
} - (void)setY:(CGFloat)y{
CGRect frame = self.frame;
frame.origin.y = y;
self.frame = frame;
} //origin;
- (CGPoint)origin{
return self.frame.origin;
} - (void)setOrigin:(CGPoint)origin{
CGRect frame = self.frame;
self.origin = origin;
self.frame = frame;
} //中心点坐标 centerX,centerY
//centerX;
- (CGFloat)centerX{
return self.center.x;
} - (void)setCenterX:(CGFloat)centerX{
CGPoint center = self.center;
center.x = centerX;
self.center = center;
} //centerY;
- (CGFloat)centerY{
return self.center.y;
} - (void)setCenterY:(CGFloat)centerY{
CGPoint center = self.center;
center.y = centerY;
self.center = center;
} //大小 ,
//width;
- (CGFloat)width{
return self.frame.size.width;
} - (void)setWidth:(CGFloat)width{
CGRect frame = self.frame;
frame.size.width = width;
self.frame = frame;
} //height;
- (CGFloat)height{
return self.frame.size.height;
} - (void)setHeight:(CGFloat)height{
CGRect frame = self.frame;
frame.size.height = height;
self.frame = frame;
} //size;
- (CGSize)size{
return self.frame.size;
} - (void)setSize:(CGSize)size{
CGRect frame = self.frame;
frame.size = size;
self.frame = frame;
} @end

应用举例:

     //修改宽
aview.width = ;
//修改x坐标
aview.x = ;
//修改y坐标
aview.y = ;

原文参考:http://mp.weixin.qq.com/s?__biz=MzA3NzM0NzkxMQ==&mid=216102953&idx=2&sn=703281ec344cc6fdb5b52f681002e255&scene=23&srcid=1018RAYFkM4iK97OMvC1c2PP#rd

上一篇:POJ 2524 :Ubiquitous Religions


下一篇:windows server 修改远程桌面连接端口号