星空网 > 软件开发 > 操作系统

IOS 杂笔-12(类别de巧用 有便于Frame的操作)

在实际开发中很多时候我们都为了控件frame的操作焦头烂额。

例如:我们只想要获取view的width。

我们可以这么操作:view.frame.size.width

有时我们想要改变view的width然而我们不能直接改变->需要三部曲。

让人抓狂,为了解决这里烦恼我们可以通过改变类别来达到理想的效果。

下面是类别的.h文件:

//// UIView+CXExtension.h////// Created by ma c on 16/3/25.// Copyright © 2016年 xubaoaichiyu. All rights reserved.//#import <UIKit/UIKit.h>@interface UIView (CXExtension)@property (nonatomic, assign) CGSize size;@property (nonatomic, assign) CGFloat width;@property (nonatomic, assign) CGFloat height;@property (nonatomic, assign) CGFloat x;@property (nonatomic, assign) CGFloat y;@end

接下来是.m文件

//// UIView+CXExtension.m////// Created by ma c on 16/3/25.// Copyright © 2016年 xubaoaichiyu. All rights reserved.//#import "UIView+CXExtension.h"@implementation UIView (CXExtension)- (void)setSize:(CGSize)size{  CGRect frame = self.frame;  frame.size = size;  self.frame = frame;}- (CGSize)size{  return self.frame.size;}- (void)setWidth:(CGFloat)width{  CGRect frame = self.frame;  frame.size.width = width;  self.frame = frame;}- (void)setHeight:(CGFloat)height{  CGRect frame = self.frame;  frame.size.height = height;  self.frame = frame;}- (void)setX:(CGFloat)x{  CGRect frame = self.frame;  frame.origin.x = x;  self.frame = frame;}- (void)setY:(CGFloat)y{  CGRect frame = self.frame;  frame.origin.y = y;  self.frame = frame;}- (CGFloat)width{  return self.frame.size.width;}- (CGFloat)height{  return self.frame.size.height;}- (CGFloat)x{  return self.frame.origin.x;}- (CGFloat)y{  return self.frame.origin.y;}@end

复制粘贴即可使用,也可以改变为其他控价。




原标题:IOS 杂笔-12(类别de巧用 有便于Frame的操作)

关键词:IOS

IOS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

马来西亚花卉节:https://www.goluckyvip.com/tag/37748.html
马来西亚货运专线:https://www.goluckyvip.com/tag/37749.html
Mercari:https://www.goluckyvip.com/tag/3775.html
马来西亚节日:https://www.goluckyvip.com/tag/37750.html
马来西亚节日大全:https://www.goluckyvip.com/tag/37751.html
马来西亚空运专线:https://www.goluckyvip.com/tag/37752.html
初学者 FACEBOOK 跟踪完整指南:https://www.xlkjsw.com/news/90211.html
快时尚品牌ASOS上半年销售额15亿英镑,业绩全面缩水:https://www.kjdsnews.com/a/1842067.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流