在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. API功能测试如何实施(转载自测试之道 微信公众号)

    什么是API 关于定义什么的,直接维基可得: API(Application Programming Interface,简称:API),又称为应用编程接口,就是软件系统不同组成部分衔接的约定.由于近 ...

  2. BBSXP最新漏洞 简单注入检測 万能password

    BBSXP最新漏洞 漏洞日期:2005年7月1日受害版本号:眼下全部BBSXP漏洞利用:查前台password注入语句:blog.asp?id=1%20union%20select%201,1,use ...

  3. 【52】写了placement new也要写placement delete

    1.Widget* pw = new Widget; 调用了两个方法:第一个方法是operator new 负责分配内存:第二个方法是在分配的内存上构造Widget,即调用Widget的default ...

  4. 理解shared_ptr<T> ---2

    1.引用计数字段不能放在资源管理类中.我们的解决办法是,把引用计数和资源绑在一起,进行二次封装.但是这样存在一个大问题,不同类型的资源管理类不能兼容.也就是说,shared_ptr<Dog> ...

  5. git reset and git checkout

    git reset --hard <commit>: 1.替换引用的指向.引用指向新的提交ID; 2.替换暂存区.替换后,暂存区的内容和引用指向的文件夹树一致; 3.替换工作区.替换后,工 ...

  6. Android蓝牙操作笔记

    蓝牙是一种支持设备短距离传输数据的无线技术.android在2.0以后提供了这方面的支持. 从查找蓝牙设备到能够相互通信要经过几个基本步骤(本机做为服务器): 1.设置权限 在manifest中配置 ...

  7. protocol buffer的简单使用

    protocol buffer是一个高效的结构化数据存储格式,用来结构化数据的序列化与反序列化.目前支持java.c++.Python 相对于json而言: 数据量跟小 其他的还没看出什么优势 下载地 ...

  8. 离线安装Cloudera Manager5.3.4与CDH5.3.4(二)

    Cloudera Manager Server和Agent所有后发先至.也能够进行CDH5的安装和配置. 然后,主节点可以通过浏览器访问7180port测试(因为CM Server需要花时间来启动,可 ...

  9. MongoDB入门简单介绍

    有关于MongoDB的资料如今较少,且大多为英文站点,以上内容大多由笔者翻译自官网,请翻译或理解错误之处请指证.之后笔者会继续关注MongoDB,并翻译“Developer Zone”和“Admin ...

  10. innobackupex --slave-info参数的含义和适用场景

    http://blog.itpub.net/28916011/viewspace-1969135/       我有个问题一直没弄明白,就是innobackupex里面的--slave-info这个参 ...