快速修改frame

快速修改UIView的frame

本篇是本人写的第一篇博客, 博客内容借鉴了简书(@sunljz)的文章, 写博客的目的是整理自己学习的内容, 同时也锻炼自己.

UIView的frame是我们开发中最熟悉不过的属性之一了, 而我们在开发过程中经常需要对设置好的frame进行改变. 但是修改frame非常麻烦, frame是CGRect类型的, 而CGRect是结构体, 结构体类型里是不允许修改单个属性的, 那么我们想修改frame的属性的话, 需要三个步骤, 1.先把frame取出来 2.修改frame 3.重新赋值回去.

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 100, 100)];
  CGRect frame = view.frame;
  frame.size.width += 50;
  view.frame = frame;

如果frame的单个属性可以直接修改, 那么使用起来会很方便, 代码看起来也很整洁.其实这时我们给UIView加个类别就可以达到这种效果了.
在类别中声明属性 :

@interface UIView (Layout)

@property (nonatomic, assign) CGFloat top;         /< 上方位置 */
@property (nonatomic, assign) CGFloat bottom;       /
< 底部位置 */
@property (nonatomic, assign) CGFloat left;         /< 左边位置 */
@property (nonatomic, assign) CGFloat right;       /
< 右边位置 */

@property (nonatomic, assign) CGFloat x;           /< x坐标 */
@property (nonatomic, assign) CGFloat y;           /
< y坐标 */
@property (nonatomic, assign) CGPoint origin;       /< 起始点 */

@property (nonatomic, assign) CGFloat centerX;     /
< X的中心位置 */
@property (nonatomic, assign) CGFloat centerY;     /< Y的中心位置 */

@property (nonatomic, assign) CGFloat width;       /
< 宽度 */
@property (nonatomic, assign) CGFloat height;       /< 高度 */
@property (nonatomic, assign) CGSize size;         /
< 大小 */


@end

然后在.m中重写它们的getter和setter方法:

@implementation UIView (Layout)

@dynamic top;
@dynamic bottom;
@dynamic left;
@dynamic right;

@dynamic x;
@dynamic y;

@dynamic width;
@dynamic height;

@dynamic size;

  • (void)setTop:(CGFloat)top{

      CGRect frame = self.frame;
      frame.origin.y = top;
      self.frame = frame;
    }
  • (CGFloat)top{

      return self.frame.origin.y;
    }
  • (void)setBottom:(CGFloat)bottom{

      CGRect frame = self.frame;
      frame.origin.y = bottom - frame.size.height;
      self.frame = frame;
    }
  • (CGFloat)bottom{

      return self.frame.origin.y + self.frame.size.height;
    }
  • (void)setLeft:(CGFloat)left{

      CGRect frame = self.frame;
      frame.origin.x = left;
      self.frame = frame;

    }
  • (CGFloat)left{

      return self.origin.x;
    }
  • (void)setRight:(CGFloat)right{

      CGRect frame = self.frame;
      frame.origin.x = right - self.frame.size.width;
      self.frame = frame;
    }
  • (CGFloat)right{

      return self.frame.size.width + self.frame.origin.x;
    }
  • (void)setX:(CGFloat)x{

      CGRect frame = self.frame;
      frame.origin.x = x;
      self.frame = frame;
    }
  • (CGFloat)x{

      return self.frame.origin.x;
    }
  • (void)setY:(CGFloat)y{

      CGRect frame = self.frame;
      frame.origin.y = y;
      self.frame = frame;
    }
  • (CGFloat)y{

      return self.frame.origin.y;
    }
  • (void)setCenterX:(CGFloat)centerX{

      CGPoint center = self.center;
      center.x = centerX;
      self.center = center;
    }
  • (CGFloat)centerX{

      return self.center.x;
    }
  • (void)setCenterY:(CGFloat)centerY{

      CGPoint center = self.center;
      center.y = centerY;
      self.center = center;
    }
  • (CGFloat)centerY{

      return self.center.y;
    }
  • (void)setOrigin:(CGPoint)origin{

      CGRect frame = self.frame;
      frame.origin = origin;
      self.frame = frame;
    }
  • (CGPoint)origin{

      return self.frame.origin;
    }
  • (void)setWidth:(CGFloat)width{

      CGRect frame =self.frame;
      frame.size.width = width;
      self.frame = frame;
    }
  • (CGFloat)width{

      return self.frame.size.width;
    }
  • (void)setHeight:(CGFloat)height{

      CGRect frame = self.frame;
      frame.size.height = height;
      self.frame = frame;
    }
  • (CGFloat)height{

      return self.frame.size.height;
    }
  • (void)setSize:(CGSize)size{

      CGRect frame = self.frame;
      frame.size = size;
      self.frame = frame;
    }
  • (CGSize)size{

      return self.frame.size;
    }

    @end

在我们需要的时候只要包含头文件 :

import "UIView+Layout.h"

就可以轻松的修改frame单个的属性了 :

view.height = 200;
view.x = 100;

posted on 2015-08-06 19:55  _Onecode  阅读(200)  评论(0)    收藏  举报

导航