iOS UIView 快速修改 frame,
在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,的更多相关文章
- iOS UIView 快速修改 frame
我们修改frame中的某个值,需要进行繁琐的书写,例如: (1). 直接设置位置大小 view.frame = CGRectMake(0, 0, 320, 150); (2). 只修改某个值 view ...
- ios 中直接修改frame里边某个属性的简便方法
参考:http://www.cnblogs.com/wengzilin/p/4359865.html 在iOS中view的frame属性使用地太频繁了,尤其是调UI的时候.我们知道,正常情况下我们无法 ...
- 【原】iOS:一种直接修改frame的某个属性的方法
在iOS中view的frame属性使用地太频繁了,尤其是调UI的时候.我们知道,正常情况下我们无法对frame的某个属性(x,y,width,height等)进行单独修改,比如: someView.f ...
- iOS 使用xib定义一个View,修改frame无效问题解决
遇到过好多次使用自定义view,修改frame无效问题, 之前都是放弃xib,直接手写,发现手写简单的还行,复杂的UI就坑逼了.所以还是需要用到可视化编辑的xib. 整理一下,自己备忘也供iOS开发的 ...
- iOS Webview 实现修改javascript confirm 和 alert
贴代码: @interface UIWebView (JavaScriptAlert) -(void) webView:(UIWebView *)sender runJavaScriptAlertPa ...
- IOS UIView圆角,阴影,边框,渐增光泽
圆角 sampleView.layer.cornerRadius = 2.5; // 圓角的弧度sampleView.layer.masksToBounds = YES; 阴影 sampleView. ...
- [转]IOS UIView 之属性篇
[转载自:IOS UIView 之属性篇 From CSDN] UIView 继承于UIResponder 所遵守的协议有 NSCoding .UIAppearance. UI ...
- cmd命令快速修改dns
新建cmd文件,修改红色ip部分,以 ANSI 编码保存,双击运行即可快速修改dns配置 netsh interface ip set dns "本地连接" source=stat ...
- word2010中怎样快速修改同级标题格式
我要把所有三级目录的字体增大,怎样能一次选中批量修改?文章很长,一百多个三级标题.word 2010中提供了快速修改的方法: ①将光标定位在一个三级标题中② <IGNORE_JS_OP> ...
随机推荐
- hdoj 2178 猜数字
猜数字 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- Qt Creator编辑器乱问题
新安装的Qt Creator 打开原来的工程源码时提示:无法用 "UTF-8"-编码解码 "main.cpp". 无法编辑 解决办法:修改项目属性的编辑器设 ...
- 关于七牛云存储,HTTPS资源上传不成功问题
关于七牛云存储,HTTPS资源上传不成功问题 官方给出了一个解决方案,亲测可用.特此记录一下. 找到QNConfiguration.m文件.然后重写两个方法,直接上代码. + (instancetyp ...
- 表单,css
- 5-17 Hashing (25分)
The task of this problem is simple: insert a sequence of distinct positive integers into a hash tabl ...
- C++学习笔记(四):枚举
枚举用来代替静态常量,优点就是可以确定值的范围,而常量则无法确定范围: 常量表示法: ; ; ; ; ; bool func(int type) { //范围检查 || type > ) thr ...
- c#后台修改前台DOM的css属性
<div id = 'div1' runat="server">haha</div> ----------- 后台代码中这样调用 div1.Style[&q ...
- .NET常用操作小知识
一..NET截取指定长度汉字超出部分以“.....”表示 /// <summary> /// 将指定字符串按指定长度进行剪切, /// </summary> /// <p ...
- 如何关闭UINavigationController 向右滑动 返回上一层视图
说明一下: 我的nav 设置的rootview 是 tabbarcontroller,登录界面是push进去的,所以,在登录界面,如果靠近最左边 向右滑动 会出现 tabbarcontroller的视 ...
- [React Fundamentals] Development Environment Setup
In this lesson we'll setup a simple build process for converting our ES6 React components into ES5 u ...