在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. Handlebar

    1.Handlebar中文网: http://www.ghostchina.com/introducing-the-handlebars-js-templating-engine/ 2.http:// ...

  2. thinkPHP配置项

    'URL_PATHINFO_DEPR'=>'-',//修改URL的分隔符 'TMPL_L_DELIM'=>'<{', //修改左定界符 'TMPL_R_DELIM'=>'}&g ...

  3. 分布式模式之broker模式

    转自:http://blog.chinaunix.net/uid-23093301-id-90459.html 问题来源: 创建一个游戏系统,其将运行在互联网的环境中.客户端通过WWW服务或特定的客户 ...

  4. C#基础知识回顾-- 反射(3)

    C#基础知识回顾-- 反射(3)   获取Type对象的构造函数: 前一篇因为篇幅问题因为篇幅太短被移除首页,反射这一块还有一篇“怎样在程序集中使用反射”, 其他没有什么可以写的了,前两篇主要是铺垫, ...

  5. Nginx加多个tomcat实现负载均衡,动静分离

    一:Nginx+Tomcat的动静分离 所谓动静分离就是通过nginx(或apache等)来处理用户端请求的图片.html等静态的文件,tomcat(或weblogic)处理jsp.do等动态文件,从 ...

  6. struts2学习笔记(5)---自己定义拦截器

    什么是拦截器? struts2中拦截器分为Struts2定义好的拦截器和自己定义的拦截器. 其作用是在一个Action运行之前进行拦截,在Action运行之后又增加某些操作. 实现原理 当请求一个Ac ...

  7. javascript 引擎Rhino源代码分析 浅析 实例函数对象及this

    http://blog.csdn.net/liantian_wu/article/details/49797481

  8. C#_Ajax_分页

    using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace MvcTe ...

  9. CLI Console

    CLI Console New to 3.0 is a command line utility aptly named Nova located in the root. It currently ...

  10. 打造强大的BaseModel(1):让Model自我描述

    前言 从事iOS开发已经两年了,从一无所知到现在能独立带领团队完成一系列APP的开发,网络上的大神给了我太多的帮助.他们无私地贡献自己的心得和经验,写出了一篇篇精美的文章.现在我也开始为大家贡献自己的 ...