一、引言:

在ios开发中,就界面搭建、控件布局时,都会很恶心的通过很长的代码才能取出控件的x、y、height、width等值,大大降低了开发效率。那为了省略这些恶心的步骤,小编在这里给UIView添加了一个关于frame的扩展分类。

二、添加了分类之后的效果对比

1、未添加分类前取值:

    self.view.frame.size.width;
self.view.frame.size.height;
self.view.frame.origin.x;
self.view.frame.origin.y;

2、添加了分类之后取值:

    self.view.height;
self.view.width;
self.view.x;
self.view.y;
self.view.size;

三、添加分类操作:

1、添加分类:

说明:在分类中通过@property添加属性时,只能声明属性及其set、get方法,但没有实现其set、get方法,需要自己实现:set、get方法

#import <UIKit/UIKit.h>

@interface UIView (ICKExtensionOfFrame)

@property (nonatomic,assign) CGFloat x;
@property (nonatomic,assign) CGFloat y;
@property (nonatomic,assign) CGFloat width;
@property (nonatomic,assign) CGFloat height;
@property (nonatomic,assign) CGSize size; @end
 #import "UIView+ICKExtensionOfFrame.h"

 @implementation UIView (ICKExtensionOfFrame)
/**
* x 的set\get方法实现
*/
- (void)setX:(CGFloat)x
{
CGRect rect = self.frame;
rect.origin.x = x;
self.frame = rect;
}
- (CGFloat)x
{
return self.frame.origin.x;
} /**
* y 的set\get方法实现
*/
- (void)setY:(CGFloat)y
{
CGRect rect = self.frame;
rect.origin.y = y;
self.frame = rect;
}
- (CGFloat)y
{
return self.frame.origin.y;
} /**
* width 的set\get方法实现
*/
- (void)setWidth:(CGFloat)width
{
CGRect rect = self.frame;
rect.size.width = width;
self.frame = rect;
}
- (CGFloat)width
{
return self.frame.size.width;
} /**
* height 的set\get方法实现
*/
- (void)setHeight:(CGFloat)height
{
CGRect rect = self.frame;
rect.size.height = height;
self.frame = rect;
}
- (CGFloat)height{
return self.frame.size.height;
} /**
* size 的set\get方法实现
*/
- (void)setSize:(CGSize)size
{
CGRect rect = self.frame;
rect.size = size;
self.frame = rect;
}

2、将其头文件导入项目的.pch

#import "UIView+ICKExtensionOfFrame.h"

3、在此项目中的任何控件的frame的属性取值,都可以直接取出。

UIView的frame的扩展分类,轻松取出x、y、height、width等值的更多相关文章

  1. PHP.38-TP框架商城应用实例-后台14-商品管理-商品扩展分类的删除、修改

    商品分类删除 1.删除商品时,根据商品id删除扩展分类表数据 商品扩展分类修改 1.在控制器GoodsController.class.php/edit()中根据商品id取出对应的所有扩展分类 2.在 ...

  2. PHP.37-TP框架商城应用实例-后台13-商品管理-扩展分类的添加、显示【数据分组】、搜索分类【多对多】

    商品扩展分类 需求:一件商品能有多个扩展分类,搜索任何一个分类都能搜出该商品 建表[扩展分类表] drop table if exists p39_goods_cat; create table p3 ...

  3. ios开发之UIView的frame、bounds跟center属性的区别(附图)

    博文暂时想到什么写什么,不顺理成章,不顺章成篇. 先看几个概念 坐标点Poit:向右侧为X轴正方向的值x,原点下侧为Y轴正方向的值y 大小Size:由宽度width和高度height构成,表示一个矩形 ...

  4. UIView的frame和bounds区别

    UIView的frame和bounds区别 iOS中,大家肯定对view和frame都不陌生,我们设置view在父view中的位置和大小时,只需要设置frame就可以了. 可能大家也有查过网上的一些资 ...

  5. UIView 中 frame, bounds, center 属性的关系

    最近一直在学 iOS 开发,所以专门创建了这样一个类别,将自己学习中的一些问题整理,记录下来.由于自己是初学者,所以所写的文章非常基础,写这个类别一是为了给自己留下存 档,二是为了给和我有同样问题的初 ...

  6. ecshop后台管理显示扩展分类

    ecshop 后台商品列表默认只显示分类下的商品,而不显示扩展分类中的商品,以下是我个人给出的解决方法: 打开admin/includes/lib_goods.php 第839行左右的位置 可以看到如 ...

  7. [UIKit学习]01.关于UIView,frame,bounds,center

    UIView是Cocoa大多控件的父类,本身不带事件. UIView的常见用法 @property(nonatomic,readonly) UIView *superview; 获得自己的父控件对象 ...

  8. swift关于UIView设置frame值的extension

    swift关于UIView设置frame值的extension 使用 说明 1. 使用如上图,很简单,不再赘述 2. 在extension给添加的计算属性提供getter,setter方法即可 源码 ...

  9. CALayer的position,anchorPoint属性 与UIView的frame 属性

    彻底理解CALayer的position,anchorPoint属性 与UIView的frame 属性 一.position,anchorPoint两者都是CALayer的属性,都是CGPoint点 ...

随机推荐

  1. Codeforces Round #312 (Div. 2) A. Lala Land and Apple Trees 暴力

    A. Lala Land and Apple Trees Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/cont ...

  2. ProgressCircular

    https://github.com/eltld/ProgressCircular

  3. android开发环境 eclipse + android sdk配置笔记

    本开发环境为:eclipse + android sdk,步骤说明的顺序,没有特别要求,看个人爱好了 步骤说明: 1.安装eclipse 2.配置jdk 3.安装android sdk 4.安装ADT ...

  4. 即时通信(RPC)的Rtmp实现--代码实现篇

    实现的一般步骤是: step 1: 定义NetConnection对象连接rtmp,并监听NetStatusEvent.NET_STATUS事件 step 2: 在NetStatusEvent.NET ...

  5. TP复习2

    ## ThinkPHP 3.1.2 输出和模型使用#讲师:赵桐正微博:http://weibo.com/zhaotongzheng 本节课大纲:M() 等效为 new Model();$m=M('Us ...

  6. alue of type java.lang.String cannot be converted to JSONObject

    /** * 4.0以下系统处理掉返回json的BOM头 * * @param jsonStr * @return */ public static String getJson(String json ...

  7. andorid

    js内存泄露 三分面加七分水 —— 十分糊涂 膝盖上打瞌睡 —— 自己靠自己 不当家,不知柴米贵:不生子,不知父母恩. 水落现石头,日久见人心. sqllite http://wenku.baidu. ...

  8. ubuntu搭建LAMP服务器

    新手记录下...... 安装apache apt-get install apache2 安装mysql apt-get install mysql-server 安装php apt-get inst ...

  9. Line in和Mic in的区别及使用

    Line in和Mic in的区别及使用 我们的电脑声卡上,一般都会有Line in和Mic in两个接口,翻译成中文就是“线性输入”和“麦克风输入”,这两个都是输入端口,但是还是有区别的: 1.Li ...

  10. .net 后台获取当前请求的设备

    检查当前发起请求的设备是手持设备还是电脑端  以便显示不同的视图 public static bool CheckIsMobile(HttpRequestBase req) { bool flag = ...