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

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

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

 //
// UIView+Layout.h
// Layout
//
// Created by Ager on 15/10/18.
// Copyright © 2015年 Ager. All rights reserved.
// #import <UIKit/UIKit.h> @interface UIView (Layout) //顶,底,左,右
@property (nonatomic , assign)CGFloat top;
@property (nonatomic , assign)CGFloat bottom;
@property (nonatomic , assign)CGFloat left;
@property (nonatomic , assign)CGFloat right; //坐标,x,y
@property (nonatomic , assign)CGFloat x;
@property (nonatomic , assign)CGFloat y;
@property (nonatomic , assign)CGPoint origin; //中心点坐标 centerX,centerY
@property (nonatomic , assign)CGFloat centerX;
@property (nonatomic , assign)CGFloat centerY; //大小 ,宽,高
@property (nonatomic , assign)CGFloat width;
@property (nonatomic , assign)CGFloat height;
@property (nonatomic , assign)CGSize size; @end

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

 //
// UIView+Layout.m
// Layout
//
// Created by Ager on 15/10/18.
// Copyright © 2015年 Ager. All rights reserved.
// #import "UIView+Layout.h" @implementation UIView (Layout) //顶,底,左,右
//top;
- (CGFloat)top{
return self.frame.origin.y;
} - (void)setTop:(CGFloat)top{
CGRect frame = self.frame;
frame.origin.y = top;
self.frame = frame;
} //bottom;
- (CGFloat)bottom{
return CGRectGetMaxY(self.frame);
} - (void)setBottom:(CGFloat)bottom{
CGRect frame = self.frame;
frame.origin.y = [self bottom] - [self height];
self.frame = frame;
} //left;
- (CGFloat)left{
return self.frame.origin.x;
} - (void)setLeft:(CGFloat)left{
CGRect frame = self.frame;
frame.origin.x = left;
self.frame = frame;
} //right;
- (CGFloat)right{
return CGRectGetMaxX(self.frame);
} - (void)setRight:(CGFloat)right{
CGRect frame = self.frame;
frame.origin.x = [self right] - [self width];
self.frame = frame;
} //坐标,x,y
//x;
- (CGFloat)x{
return self.frame.origin.x;
} - (void)setX:(CGFloat)x{
CGRect frame = self.frame;
frame.origin.x = x;
self.frame = frame;
} //y; - (CGFloat)y{
return self.origin.y;
} - (void)setY:(CGFloat)y{
CGRect frame = self.frame;
frame.origin.y = y;
self.frame = frame;
} //origin;
- (CGPoint)origin{
return self.frame.origin;
} - (void)setOrigin:(CGPoint)origin{
CGRect frame = self.frame;
self.origin = origin;
self.frame = frame;
} //中心点坐标 centerX,centerY
//centerX;
- (CGFloat)centerX{
return self.center.x;
} - (void)setCenterX:(CGFloat)centerX{
CGPoint center = self.center;
center.x = centerX;
self.center = center;
} //centerY;
- (CGFloat)centerY{
return self.center.y;
} - (void)setCenterY:(CGFloat)centerY{
CGPoint center = self.center;
center.y = centerY;
self.center = center;
} //大小 ,
//width;
- (CGFloat)width{
return self.frame.size.width;
} - (void)setWidth:(CGFloat)width{
CGRect frame = self.frame;
frame.size.width = width;
self.frame = frame;
} //height;
- (CGFloat)height{
return self.frame.size.height;
} - (void)setHeight:(CGFloat)height{
CGRect frame = self.frame;
frame.size.height = height;
self.frame = frame;
} //size;
- (CGSize)size{
return self.frame.size;
} - (void)setSize:(CGSize)size{
CGRect frame = self.frame;
frame.size = size;
self.frame = frame;
} @end

应用举例:

     //修改宽
aview.width = ;
//修改x坐标
aview.x = ;
//修改y坐标
aview.y = ;

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

iOS UIView 快速修改 frame,的更多相关文章

  1. iOS UIView 快速修改 frame

    我们修改frame中的某个值,需要进行繁琐的书写,例如: (1). 直接设置位置大小 view.frame = CGRectMake(0, 0, 320, 150); (2). 只修改某个值 view ...

  2. ios 中直接修改frame里边某个属性的简便方法

    参考:http://www.cnblogs.com/wengzilin/p/4359865.html 在iOS中view的frame属性使用地太频繁了,尤其是调UI的时候.我们知道,正常情况下我们无法 ...

  3. 【原】iOS:一种直接修改frame的某个属性的方法

    在iOS中view的frame属性使用地太频繁了,尤其是调UI的时候.我们知道,正常情况下我们无法对frame的某个属性(x,y,width,height等)进行单独修改,比如: someView.f ...

  4. iOS 使用xib定义一个View,修改frame无效问题解决

    遇到过好多次使用自定义view,修改frame无效问题, 之前都是放弃xib,直接手写,发现手写简单的还行,复杂的UI就坑逼了.所以还是需要用到可视化编辑的xib. 整理一下,自己备忘也供iOS开发的 ...

  5. iOS Webview 实现修改javascript confirm 和 alert

    贴代码: @interface UIWebView (JavaScriptAlert) -(void) webView:(UIWebView *)sender runJavaScriptAlertPa ...

  6. IOS UIView圆角,阴影,边框,渐增光泽

    圆角 sampleView.layer.cornerRadius = 2.5; // 圓角的弧度sampleView.layer.masksToBounds = YES; 阴影 sampleView. ...

  7. [转]IOS UIView 之属性篇

    [转载自:IOS UIView 之属性篇 From CSDN] UIView 继承于UIResponder             所遵守的协议有 NSCoding .UIAppearance. UI ...

  8. cmd命令快速修改dns

    新建cmd文件,修改红色ip部分,以 ANSI 编码保存,双击运行即可快速修改dns配置 netsh interface ip set dns "本地连接" source=stat ...

  9. word2010中怎样快速修改同级标题格式

    我要把所有三级目录的字体增大,怎样能一次选中批量修改?文章很长,一百多个三级标题.word 2010中提供了快速修改的方法: ①将光标定位在一个三级标题中② <IGNORE_JS_OP> ...

随机推荐

  1. UVa123 - Searching Quickly

    题目地址:点击打开链接 C++代码: #include <iostream> #include <set> #include <map> #include < ...

  2. 重看Decorator Pattern,联想到Delegate传递及Flags Enum--欢迎拍砖!

    话说装饰模式(Decorator)的动机是“动态地给一个对象添加一些额外的职责.就增加功能来说,Decorator模式相比生成子类更为灵活.[GOF <设计模式>]”.再次学到该模式,有感 ...

  3. git 快速入门(二)

    一.引子 git代码托管的优秀工具之一, 其工作原理和svn截然不同.一旦拥有主干master分支权限, 只要在本地拉取主干分支, 可以随时随地切换分支. 它拥有众多优点,eg :支持在断网的情况下, ...

  4. linux mail命令用法

    在Linux系统下mail命令的测试 1. 最简单的一个例子: mail -s test admin@aispider.com 这条命令的结果是发一封标题为test的空信给后面的邮箱,如果你有mta并 ...

  5. cocos2d-x Sprite

    转自:http://codingnow.cn/cocos2d-x/795.html 精灵是游戏中十分重要的组成部分,随处可见,如:游戏背景.NPC.人物.道具等.在cocos2d-x引擎中,只要是用图 ...

  6. 免费开放的API

    1)新浪IP地址查询API接口 http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=IP地址 format有 js. ...

  7. 黑马程序猿_7K面试题之交通灯系统

    交通灯信号模拟系统 一.概述 模拟实现十字路口的交通灯管理系统逻辑,详细需求例如以下:(需求直接来源于老师的文档) ①      异步随机生成依照各个路线行驶的车辆. 比如: 由南向而来去往北向的车辆 ...

  8. Codeforces Round #325 (Div. 2) A. Alena's Schedule 水题

    A. Alena's Schedule Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/586/pr ...

  9. 设计模式之十三:适配器模式(Adapter)

    适配器模式: 将一个类的接口转换成另外一个期望的类的接口.适配器同意接口互不兼容的类一起工作. Convert the interface of a class into another interf ...

  10. 期望-pku-oj-1055:Tree

    题目链接: http://poj.openjudge.cn/practice/1055/ 题目意思: 给出的树最大节点个数为n的情况下,求树上点深度的期望. 解题思路: 数学期望公式的推导. 自己先画 ...