关于UIView的位置都会遇到,一般需要改变UIView的位置,需要先获取原有的frame位置,然后在frame上面修改,有的时候如果只是改变了一下垂直方向的位置,宽度和高度的一种,这种写法很麻烦。下面两种写法第二种明显更简单,如果需要实现第二种方法就需要扩展UIView。

    //1
CGRect frame=self.testView.frame;
frame.size.width=120;
self.testView.frame=frame;
[self printFrame];
//2
self.testView.width=120;
[self printFrame];

扩展定义:

@interface UIView (ReSize)

@property (nonatomic, assign) CGSize size;

@property (nonatomic,assign)  CGFloat x;

@property  (nonatomic,assign) CGFloat y;

@property (nonatomic, assign) CGFloat top;

@property (nonatomic, assign) CGFloat bottom;

@property (nonatomic, assign) CGFloat left;

@property (nonatomic, assign) CGFloat right;

@property (nonatomic, assign) CGFloat centerX;

@property (nonatomic, assign) CGFloat centerY;

@property (nonatomic, assign) CGFloat width;

@property (nonatomic, assign) CGFloat height;

@end

 扩展实现:

@implementation UIView (ReSize)

- (CGSize)size;
{
return [self frame].size;
} - (void)setSize:(CGSize)size;
{
CGPoint origin = [self frame].origin;
[self setFrame:CGRectMake(origin.x, origin.y, size.width, size.height)];
} -(CGFloat)x{
CGRect frame=[self frame];
return frame.origin.x;
} -(void)setX:(CGFloat)x{
CGRect frame=[self frame];
frame.origin.x=x;
[self setFrame:frame];
} -(CGFloat)y{
CGRect frame=[self frame];
return frame.origin.y;
} -(void)setY:(CGFloat)y{
CGRect frame=[self frame];
frame.origin.y=y;
return [self setFrame:frame];
} - (CGFloat)left;
{
return CGRectGetMinX([self frame]);
} - (void)setLeft:(CGFloat)x;
{
CGRect frame = [self frame];
frame.origin.x = x;
[self setFrame:frame];
} - (CGFloat)top;
{
return CGRectGetMinY([self frame]);
} - (void)setTop:(CGFloat)y;
{
CGRect frame = [self frame];
frame.origin.y = y;
[self setFrame:frame];
} - (CGFloat)right;
{
return CGRectGetMaxX([self frame]);
} - (void)setRight:(CGFloat)right;
{
CGRect frame = [self frame];
frame.origin.x = right - frame.size.width; [self setFrame:frame];
} - (CGFloat)bottom;
{
return CGRectGetMaxY([self frame]);
} - (void)setBottom:(CGFloat)bottom;
{
CGRect frame = [self frame];
frame.origin.y = bottom - frame.size.height;
[self setFrame:frame];
} - (CGFloat)centerX;
{
return [self center].x;
} - (void)setCenterX:(CGFloat)centerX;
{
[self setCenter:CGPointMake(centerX, self.center.y)];
} - (CGFloat)centerY;
{
return [self center].y;
} - (void)setCenterY:(CGFloat)centerY;
{
[self setCenter:CGPointMake(self.center.x, centerY)];
} - (CGFloat)width;
{
return CGRectGetWidth([self frame]);
} - (void)setWidth:(CGFloat)width;
{
CGRect frame = [self frame];
frame.size.width = width;
[self setFrame:CGRectStandardize(frame)];
} - (CGFloat)height;
{
return CGRectGetHeight([self frame]);
} - (void)setHeight:(CGFloat)height;
{
CGRect frame=[self frame];
frame.size.height = height;
[self setFrame:CGRectStandardize(frame)];
} @end

项目源代码地址:https://github.com/SmallElephant/iOS-FEViewReSize

iOS开发-UIView扩展CGRect的更多相关文章

  1. iOS开发UIView.h简介

    1.UICoordinateSpace不同坐标空间的坐标切换 @protocol UICoordinateSpace <NSObject> //将当前的坐标空间点转换到指定的坐标空间 - ...

  2. iOS 自定义方法 - UIView扩展

    示例代码 //#import <UIKit/UIKit.h>@interface UIView (LPCView)/** 上 */@property CGFloat top;/** 下 * ...

  3. iOS开发之类扩展

    在以往写代码时,我们经常是把声明写在.h文件中,把实现写在.m文件中,但是在实际开发中,如果把声明写在.h文件中会暴露程序很多属性(成员变量.成员变量的get和set方法),为了安全考虑,引入了类扩展 ...

  4. iOS开发系列--扩展--播放音乐库中的音乐

    众所周知音乐是iOS的重要组成播放,无论是iPod.iTouch.iPhone还是iPad都可以在iTunes购买音乐或添加本地音乐到音乐 库中同步到你的iOS设备.在MediaPlayer.fram ...

  5. iOS开发--打印NSRange,CGRect等结构体

    使用对应的转换NSStringFromCGPoint   NSStringFromCGSize   NSStringFromCGRect  NSStringFromCGAffineTransform  ...

  6. iOS开发——UI精选OC篇&UIApplication,UIWindow,UIViewController,UIView(layer)简单介绍

    UIApplication,UIWindow,UIViewController,UIView(layer)简单介绍 一:UIApplication:单例(关于单例后面的文章中会详细介绍,你现在只要知道 ...

  7. iOS开发系列--App扩展开发

    概述 从iOS 8 开始Apple引入了扩展(Extension)用于增强系统应用服务和应用之间的交互.它的出现让自定义键盘.系统分享集成等这些依靠系统服务的开发变成了可能.WWDC 2016上众多更 ...

  8. iOS开发UI篇—核心动画(UIView封装动画)

    iOS开发UI篇—核心动画(UIView封装动画) 一.UIView动画(首尾) 1.简单说明 UIKit直接将动画集成到UIView类中,当内部的一些属性发生改变时,UIView将为这些改变提供动画 ...

  9. iOS开发笔记10:圆点缩放动画、强制更新、远程推送加语音提醒及UIView截屏

    1.使用CAReplicatorLayer制作等待动画 CALayer+CABasicAnimation可以制作很多简单的动画效果,之前的博客中介绍的“两个动画”,一个是利用一张渐变色图片+CABas ...

随机推荐

  1. java多线程快速入门(十)

    synchonizd解决安全性问题 package com.cppdy; class MyThread6 implements Runnable{ private Integer ticketCoun ...

  2. Application.ProcessMessages; 的重要性

    https://files.cnblogs.com/files/del88/登陆光标_悬赏50元.zip ----------------------------------------------- ...

  3. LICEcap方便快捷制作gif图片的工具

    总是看见别人的博客里面动态的小图片,是不是有种冲动自己也想搞,但是就是不知道咋搞,这里简单介绍一款很实用的制作gif的软件. LICEcap的网址:http://www.cockos.com/lice ...

  4. [转] h5上传视频或文件编写

    Html5 finally solves an age old problem of being able to upload files while also showing the upload ...

  5. #3 Codeforces-865C Gotta Go Fast(期望dp)

    题意:一个游戏一共有n个关卡,对于第i关,用a[i]时间通过的概率为p[i],用b[i]通过的时间为1-p[i],每通过一关后可以选择继续下一关或者时间清0并从第一关开始,先要求通过所有关卡的时间和不 ...

  6. C# Winform将控件作为参数传递

    最近做个Winform 的程序设计,需要将窗体的控件作为参数传递到另外一个类的函数中去使用,每次都会忘记,简单的记下来,以备即时查看. 1. 设置控件的modifier属性设置为public 2. 以 ...

  7. js后退

    history.back(-1):直接返回当前页的上一页,数据全部消息,是个新页面 history.go(-1):也是返回当前页的上一页,不过表单里的数据全部还在 history.back(0) 刷新 ...

  8. Spring日记_01 之基于maven的Spring环境搭建

    阿里云镜像:maven.aliyun.com 添加Spring坐标: Spring 是java组件容器,Java饭馆 使用者可以通过getBean(对象ID) 获得Date对象,而不需要自己去new ...

  9. CentOS root用户修改密码

    1.root用户修改密码: #passwd -------------------------------- 参考资料: 1.Centos修改root密码:http://blog.163.com/wz ...

  10. 不一样的go语言-不同的语法之type

    前言   在go语言中,type用于类型定义(type definition)与类型别名(type alias).这两者的差别从名字上已经可以初见端倪.   类型定义即定义新类型,是一个全新的类型,但 ...