码迷,mamicode.com
首页 > 移动开发 > 详细

iOS UIView 快速修改 frame,

时间:2015-10-18 23:06:44      阅读:302      评论:0      收藏:0      [点我收藏+]

标签:

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

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

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

技术分享
 1 //
 2 //  UIView+Layout.h
 3 //  Layout
 4 //
 5 //  Created by Ager on 15/10/18.
 6 //  Copyright © 2015年 Ager. All rights reserved.
 7 //
 8 
 9 #import <UIKit/UIKit.h>
10 
11 @interface UIView (Layout)
12 
13 //顶,底,左,右
14 @property (nonatomic , assign)CGFloat top;
15 @property (nonatomic , assign)CGFloat bottom;
16 @property (nonatomic , assign)CGFloat left;
17 @property (nonatomic , assign)CGFloat right;
18 
19 //坐标,x,y
20 @property (nonatomic , assign)CGFloat x;
21 @property (nonatomic , assign)CGFloat y;
22 @property (nonatomic , assign)CGPoint origin;
23 
24 //中心点坐标 centerX,centerY
25 @property (nonatomic , assign)CGFloat centerX;
26 @property (nonatomic , assign)CGFloat centerY;
27 
28 
29 //大小 ,宽,高
30 @property (nonatomic , assign)CGFloat width;
31 @property (nonatomic , assign)CGFloat height;
32 @property (nonatomic , assign)CGSize size;
33 
34 @end
View Code

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

技术分享
  1 //
  2 //  UIView+Layout.m
  3 //  Layout
  4 //
  5 //  Created by Ager on 15/10/18.
  6 //  Copyright © 2015年 Ager. All rights reserved.
  7 //
  8 
  9 #import "UIView+Layout.h"
 10 
 11 @implementation UIView (Layout)
 12 
 13 //顶,底,左,右
 14 //top;
 15 - (CGFloat)top{
 16     return self.frame.origin.y;
 17 }
 18 
 19 - (void)setTop:(CGFloat)top{
 20     CGRect frame = self.frame;
 21     frame.origin.y = top;
 22     self.frame = frame;
 23 }
 24 
 25 //bottom;
 26 - (CGFloat)bottom{
 27     return CGRectGetMaxY(self.frame);
 28 }
 29 
 30 - (void)setBottom:(CGFloat)bottom{
 31     CGRect frame = self.frame;
 32     frame.origin.y = [self bottom] - [self height];
 33     self.frame = frame;
 34 }
 35 
 36 //left;
 37 - (CGFloat)left{
 38     return self.frame.origin.x;
 39 }
 40 
 41 - (void)setLeft:(CGFloat)left{
 42     CGRect frame = self.frame;
 43     frame.origin.x = left;
 44     self.frame = frame;
 45 }
 46 
 47 //right;
 48 - (CGFloat)right{
 49     return CGRectGetMaxX(self.frame);
 50 }
 51 
 52 - (void)setRight:(CGFloat)right{
 53     CGRect frame = self.frame;
 54     frame.origin.x = [self right] - [self width];
 55     self.frame = frame;
 56 }
 57 
 58 
 59 //坐标,x,y
 60 //x;
 61 - (CGFloat)x{
 62     return self.frame.origin.x;
 63 }
 64 
 65 - (void)setX:(CGFloat)x{
 66     CGRect frame = self.frame;
 67     frame.origin.x = x;
 68     self.frame = frame;
 69 }
 70 
 71 //y;
 72 
 73 - (CGFloat)y{
 74     return self.origin.y;
 75 }
 76 
 77 - (void)setY:(CGFloat)y{
 78     CGRect frame = self.frame;
 79     frame.origin.y = y;
 80     self.frame = frame;
 81 }
 82 
 83 //origin;
 84 - (CGPoint)origin{
 85     return self.frame.origin;
 86 }
 87 
 88 - (void)setOrigin:(CGPoint)origin{
 89     CGRect frame = self.frame;
 90     self.origin = origin;
 91     self.frame = frame;
 92 }
 93 
 94 
 95 //中心点坐标 centerX,centerY
 96 //centerX;
 97 - (CGFloat)centerX{
 98     return self.center.x;
 99 }
100 
101 - (void)setCenterX:(CGFloat)centerX{
102     CGPoint center = self.center;
103     center.x = centerX;
104     self.center = center;
105 }
106 
107 
108 //centerY;
109 - (CGFloat)centerY{
110     return self.center.y;
111 }
112 
113 - (void)setCenterY:(CGFloat)centerY{
114     CGPoint center = self.center;
115     center.y = centerY;
116     self.center = center;
117 }
118 
119 
120 //大小 ,
121 //width;
122 - (CGFloat)width{
123     return self.frame.size.width;
124 }
125 
126 - (void)setWidth:(CGFloat)width{
127     CGRect frame = self.frame;
128     frame.size.width = width;
129     self.frame = frame;
130 }
131 
132 
133 //height;
134 - (CGFloat)height{
135     return self.frame.size.height;
136 }
137 
138 - (void)setHeight:(CGFloat)height{
139     CGRect frame = self.frame;
140     frame.size.height = height;
141     self.frame = frame;
142 }
143 
144 //size;
145 - (CGSize)size{
146     return self.frame.size;
147 }
148 
149 
150 - (void)setSize:(CGSize)size{
151     CGRect frame = self.frame;
152     frame.size = size;
153     self.frame = frame;
154 }
155 
156 @end
View Code

应用举例:

技术分享
1     //修改宽
2     aview.width = 300;
3     //修改x坐标
4     aview.x = 100;
5     //修改y坐标
6     aview.y = 100;
View Code

 

 

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

 

iOS UIView 快速修改 frame,

标签:

原文地址:http://www.cnblogs.com/Ager/p/4890308.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!