iOS UIView 快速修改 frame
我们修改frame中的某个值,需要进行繁琐的书写,例如:
(1). 直接设置位置大小
view.frame = CGRectMake(0, 0, 320, 150);
(2). 只修改某个值
view.frame = CGRectMake(view.frame.origin.x, 100, view.frame.size.width, view.frame.size.height);
这种写法在界面元素较多的排版中,会让程序员非常痛苦,而且可读性也会减弱
如果能够人性化一点,可以单独地修改某个值,或者再人性化一点,可以输出view中每一个点的坐标,将更有利于布局。我们可以通过建立view的类别 category 来实现这个人性化的布局操作
一、建立类别
建立一个名为Layout的UIView类别,类别的代码如下:
1. UIView+Layout.h
.h 文件定义了 x, y, width, height 等方便读写的属性,代码如下:
#import <UIKit/UIKit.h>
@interface UIView (Layout)
@property (assign, nonatomic) CGFloat top;
@property (assign, nonatomic) CGFloat bottom;
@property (assign, nonatomic) CGFloat left;
@property (assign, nonatomic) CGFloat right;
@property (assign, nonatomic) CGFloat x;
@property (assign, nonatomic) CGFloat y;
@property (assign, nonatomic) CGPoint origin;
@property (assign, nonatomic) CGFloat centerX;
@property (assign, nonatomic) CGFloat centerY;
@property (assign, nonatomic) CGFloat width;
@property (assign, nonatomic) CGFloat height;
@property (assign, nonatomic) CGSize size;
@end
2. UIView+Layout.m
.m 文件实现各个属性的setter和getter方法,代码如下:
#import "UIView+Layout.h"
@implementation UIView (Layout)
@dynamic top;
@dynamic bottom;
@dynamic left;
@dynamic right;
@dynamic width;
@dynamic height;
@dynamic size;
@dynamic x;
@dynamic y;
- (CGFloat)top
{
return self.frame.origin.y;
}
- (void)setTop:(CGFloat)top
{
CGRect frame = self.frame;
frame.origin.y = top;
self.frame = frame;
}
- (CGFloat)left
{
return self.frame.origin.x;
}
- (void)setLeft:(CGFloat)left
{
CGRect frame = self.frame;
frame.origin.x = left;
self.frame = frame;
}
- (CGFloat)bottom
{
return self.frame.size.height + self.frame.origin.y;
}
- (void)setBottom:(CGFloat)bottom
{
CGRect frame = self.frame;
frame.origin.y = bottom - frame.size.height;
self.frame = frame;
}
- (CGFloat)right
{
return self.frame.size.width + self.frame.origin.x;
}
- (void)setRight:(CGFloat)right
{
CGRect frame = self.frame;
frame.origin.x = right - frame.size.width;
self.frame = frame;
}
- (CGFloat)x
{
return self.frame.origin.x;
}
- (void)setX:(CGFloat)value
{
CGRect frame = self.frame;
frame.origin.x = value;
self.frame = frame;
}
- (CGFloat)y
{
return self.frame.origin.y;
}
- (void)setY:(CGFloat)value
{
CGRect frame = self.frame;
frame.origin.y = value;
self.frame = frame;
}
- (CGPoint)origin
{
return self.frame.origin;
}
- (void)setOrigin:(CGPoint)origin
{
CGRect frame = self.frame;
frame.origin = origin;
self.frame = frame;
}
- (CGFloat)centerX
{
return self.center.x;
}
- (void)setCenterX:(CGFloat)centerX
{
CGPoint center = self.center;
center.x = centerX;
self.center = center;
}
- (CGFloat)centerY
{
return self.center.y;
}
- (void)setCenterY:(CGFloat)centerY
{
CGPoint center = self.center;
center.y = centerY;
self.center = center;
}
- (CGFloat)width
{
return self.frame.size.width;
}
- (void)setWidth:(CGFloat)width
{
CGRect frame = self.frame;
frame.size.width = width;
self.frame = frame;
}
- (CGFloat)height
{
return self.frame.size.height;
}
- (void)setHeight:(CGFloat)height
{
CGRect frame = self.frame;
frame.size.height = height;
self.frame = frame;
}
- (CGSize)size
{
return self.frame.size;
}
- (void)setSize:(CGSize)size
{
CGRect frame = self.frame;
frame.size = size;
self.frame = frame;
}
@end
二、使用
类别创建完成以后,view的布局就显得轻松很多了 例如原来你要修改y坐标时要这样写:
view.frame = CGRectMake(view.frame.origin.x, 100, view.frame.size.width, view.frame.size.height);
而现在只需要这样写:
view.y = 100;
既简洁又方便
iOS UIView 快速修改 frame的更多相关文章
- iOS UIView 快速修改 frame,
在iOS开发布局修改 frame 时需要繁琐的代码实现,今天偶尔看到一播客说到快速修改的 frame 的方法,自己动手写了一遍实现代码. 快速实现主要通过 添加类目的方式,对UIView 控件添加了一 ...
- 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> ...
随机推荐
- cocos2d-x 多线程以及线程同步
转自:http://blog.csdn.net/zhy_cheng/article/details/9116479 cocos2d-x引擎在内部实现了一个庞大的主循环,每帧之间更新界面,如果耗时的操作 ...
- 改变UIView 的位置 Center和Frame
网上找了一个,一般来说 有两种方法: 1.改变view 的Frame [cell setFrame:CGRectMake(<#CGFloat x#>, <#CGFloat y#> ...
- codeforces Gym 100500H A. Potion of Immortality 简单DP
Problem H. ICPC QuestTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100500/a ...
- Linux下进程的同步相互排斥实例——生产者消费者
linux下的同步和相互排斥 Linux sync_mutex 看的更舒服点的版本号= = https://github.com/Svtter/MyBlog/blob/master/Linux/pth ...
- node.js小工具--修改Xcode 'Create by'作者名称
简介 用Xcode创建源文件时会自动在文件开始位置加入如下注释: // // ISSImageCycleScrollView.m // SoftTravel // // Created by iss1 ...
- ural 1998 The old Padawan
先预处理每一个点往前退几步 就一个trick..要处理这一秒已经超出了要拿完所花的时间 #include <iostream> #include <cstring> #incl ...
- thinkphp中的分表方法
public function getPartitionTableName($data=array()) { // 对数据表进行分区 if(isset($data[$this->partitio ...
- Python学习 之 流程控制
1.if else 语法:if expression1: statement1(s) elif expression2: statement2(s) else: statement3(s) 2.for ...
- apache配置--虚拟目录
apache在httpd-vhosts.conf中 配置二级域名或者泛域名: <VirtualHost *:80> ServerAdmin 846606478@qq.com D ...
- 20+ Rsync command’s switches and common usages with examples – Unix/Linux--reference
reference:http://crybit.com/rsync-commands-switches/ The “rsync” is a powerful command under the Lin ...