直接修改UIView的x,y,width,height...
来源:互联网 发布:保山学院网络教学平台 编辑:程序博客网 时间:2024/05/16 19:08
在iOS开发中,我们常常需要修改一个一个控件的x,y,width,height…但是OC的语法不允许我们直接修改结构体中的成员变量,我们往往需要先取出控件的frame或者是bounds或者是center,然后进行相应地修改,再次将frame,bounds,center赋值。这样显得比较繁琐。那我们怎么做到能够直接修改UIView的x,y,width,height呢?我们知道所有的控件(我们直接或者说是常用的控件都是继承自UIView),而iOS开发中允许我们给系统的类写分类,这样我们就可以写相应地分类来解决这个问题。
首先我们建议一个名为Layout的UIView的分类。代码如下:
- UIView + Layout.h
#import <UIKit/UIKit.h>@interface UIView (Layout)@property (nonatomic,assign) float top;@property (nonatomic,assign) float bottom;@property (nonatomic,assign) float left;@property (nonatomic,assign) float right;@property (nonatomic,assign) float x;@property (nonatomic,assign) float y;@property (nonatomic,assign) float widht;@property (nonatomic,assign) float heiht;@property (nonatomic,assign) float centerX;@property (nonatomic,assign) float centerY;@property (nonatomic,assign) CGPoint origin;@property (nonatomic,assign) CGSize size;@end
- UIView + Layout.m
#import "UIView+Layout.h"@implementation UIView (Layout)- (float)top{ return self.frame.origin.y;}- (void)setTop:(float)top{ CGRect frame = self.frame; frame.origin.y = top; self.frame = frame;}- (float)bottom{ return self.frame.origin.y + self.frame.size.height;}- (void)setBottom:(float)bottom{ CGRect frame = self.frame; frame.origin.y = bottom - self.frame.size.height; self.frame = frame;}- (float)left{ return self.frame.origin.x;}- (void)setLeft:(float)left{ CGRect frame = self.frame; frame.origin.x = left; self.frame = frame;}- (float)right{ return self.frame.origin.y + self.frame.size.width;}- (void)setRight:(float)right{ CGRect frame = self.frame; frame.origin.x = right - self.frame.size.width; self.frame = frame;}- (float)x{ return self.frame.origin.x;}- (void)setX:(float)x{ CGRect frame = self.frame; frame.origin.x = x; self.frame = frame;}- (float)y{ return self.frame.origin.y;}- (void)setY:(float)y{ CGRect frame = self.frame; frame.origin.y = y; self.frame = frame;}- (float)widht{ return self.frame.size.width;}- (void)setWidht:(float)widht{ CGRect frame = self.frame; frame.size.width = widht; self.frame = frame;}- (float)heiht{ return self.frame.size.height;}- (void)setHeiht:(float)heiht{ CGRect frame = self.frame; frame.size.height= heiht; self.frame = frame;}- (float)centerX{ return self.center.x;}- (void)setCenterX:(float)centerX{ CGPoint center = self.center; center.x = centerX; self.center = center;}- (float)centerY{ return self.center.y;}- (void)setCenterY:(float)centerY{ CGPoint center = self.center; center.y = centerY; self.center = center;}- (CGSize)size{ return self.frame.size;}- (void)setSize:(CGSize)size{ CGRect frame = self.frame; frame.size = size; self.frame = frame;}- (CGPoint)origin{ return self.frame.origin;}- (void)setOrigin:(CGPoint)origin{ CGRect frame = self.frame; frame.origin= origin; self.frame = frame;}@end
这样,使用起来就十分方便了,我写了一个小程序,需要看看的请点击这里。
0 0
- 直接修改UIView的x,y,width,height...
- 修改view的x,y,width,height值的方法
- 单独设置UIView的坐标x或y或width或height
- iOS 直接改变控件x.y.width.height
- IOS UIView 的坐标扩展,更加便捷的获取View的 height Width X Y size origin 等
- opencv 中x,y,height, width,rows,cols 的关系
- iOS快速得到视图的width,height,x,y
- 修改a标签height,width的方法
- 不再纠结OpenCV图像中的x,y;width,height;cols,rows
- 如何动态修改ddraw 表面的width和height
- public void setPixels (int[] pixels, int offset, int stride, int x, int y, int width, int height)
- HTML5 Canvas 的width/height 与style.width/height
- 取得控件的width,height
- javascript 的各种 width、height
- css的height,min-height,max-height,min-width.....
- width height
- 使用CSS修改TD或TR的高度(height)或宽度(width)
- RowDefinition columnDefinition 的height width的几种值
- 强调相关的类
- 二叉搜索树
- HTML - Amazing HTML
- Linux权限
- plist苹果安装包实现
- 直接修改UIView的x,y,width,height...
- linux下头文件
- hdu5237 Base64(模拟)
- ios截图代码
- nyoj 组合数 32 (dfs)
- python mysql盲注小程序
- Android-Activity
- [iOS]隐藏NavigationBar和StatusBar
- Linux下的文件、文件夹加密方法