关于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性能分析神器-JProfiler安装和简单介绍

    Jprofiler是一个重量级的工具,需要分别在服务器和windows都装客户端,会损耗性能,用于发现问题后排查问题,而不是常规的监控 JPROFILER工具下载地址:http://www.ej-te ...

  2. 【C++ Primer 第13章】6.对象移动

    右值引用 左值和右值 (1)两者区别: ①左值:能对表达式取地址.或具名对象/变量.一般指表达式结束后依然存在的持久对象. ②右值:不能对表达式取地址,或匿名对象.一般指表达式结束就不再存在的临时对象 ...

  3. hihocoder 编程练习赛23

    第一题:H国的身份证号码I 题意:一个N位的正整数(首位不能是0).每位数字都小于等于K,并且任意相邻两位数字的乘积也小于等于K.按从小到大的顺序输出所有合法的N位号码,每个号码占一行. 思路:dfs ...

  4. zjoi 力

    显然fft维护卷积就可以了 发现fft里面会改变很多东西 要还原一下 #include <bits/stdc++.h> #define dob complex<double> ...

  5. BZOJ 3771 Triple FFT+容斥原理

    解析: 这东西其实就是指数型母函数? 所以刚开始读入的值我们都把它前面的系数置为1. 然后其实就是个多项式乘法了. 最大范围显然是读入的值中的最大值乘三,对于本题的话是12W? 用FFT优化的话,达到 ...

  6. Matrix PKU 2155

    问题描述 给定N * N矩阵A,其元素为0或1.A [i,j]表示第i行和第j列中的数字.最初我们有A [i,j] = 0(1 <= i,j <= N). 我们可以通过以下方式更改矩阵.给 ...

  7. 《Android进阶之光》--RxJava

    No1: RxJava使用 dependencies{ compile 'io.reactivex:rxjava:1.2.0' compile 'io.reactivex:rxandroid:1.2. ...

  8. vscode下Python设置参考

    用于VS代码的Python扩展是高度可配置的.此页面介绍了可以使用的关键设置. 请参阅用户和工作区设置,以了解有关在VS代码中使用设置的更多信息. 常规设置 设置 默认 描述 python.pytho ...

  9. P2376 [USACO09OCT]津贴Allowance

    P2376 [USACO09OCT]津贴Allowance一开始想的是多重背包,但是实践不了.实际是贪心,让多c尽可能少,所以先放大的,最后让小的来弥补. #include<iostream&g ...

  10. 012.Docker私有仓库多Harbor同步部署

    一 Harbor主从介绍 harbor官方默认提供主从复制的方案来解决镜像同步问题,通过复制的方式,我们可以实时将测试环境harbor仓库的镜像同步到生产环境harbor,类似于如下流程: Harbo ...