iOS UIView 快速修改 frame,,iosuiview

來源:互聯網
上載者:User

iOS UIView 快速修改 frame,,iosuiview

在iOS開發布局修改 frame 時需要繁瑣的代碼實現,今天偶爾看到一播客說到快速修改的 frame 的方法,自己動手寫了一遍實現代碼.

快速實現主要通過 添加類目的方式,對UIView 控制項添加了一些直接修改 frame 屬性的方法(如:擷取高度.寬度,座標等);具體代碼實現如下:

.h檔案,聲明要用到的屬性

1 // 2 // UIView+Layout.h 3 // Layout 4 // 5 // Created by Ager on 15/10/18. 6 // Copyright © 2015年 Ager. All rights reserved. 7 // 8 9 #import <UIKit/UIKit.h>10 11 @interface UIView (Layout)12 13 //頂,底,左,右14 @property (nonatomic , assign)CGFloat top;15 @property (nonatomic , assign)CGFloat bottom;16 @property (nonatomic , assign)CGFloat left;17 @property (nonatomic , assign)CGFloat right;18 19 //座標,x,y20 @property (nonatomic , assign)CGFloat x;21 @property (nonatomic , assign)CGFloat y;22 @property (nonatomic , assign)CGPoint origin;23 24 //中心點座標 centerX,centerY25 @property (nonatomic , assign)CGFloat centerX;26 @property (nonatomic , assign)CGFloat centerY;27 28 29 //大小 ,寬,高30 @property (nonatomic , assign)CGFloat width;31 @property (nonatomic , assign)CGFloat height;32 @property (nonatomic , assign)CGSize size;33 34 @endView Code

.m 檔案實現對 屬性 的操作.從而實現對 frame 的修改

1 // 2 // UIView+Layout.m 3 // Layout 4 // 5 // Created by Ager on 15/10/18. 6 // Copyright © 2015年 Ager. All rights reserved. 7 // 8 9 #import "UIView+Layout.h" 10 11 @implementation UIView (Layout) 12 13 //頂,底,左,右 14 //top; 15 - (CGFloat)top{ 16 return self.frame.origin.y; 17 } 18 19 - (void)setTop:(CGFloat)top{ 20 CGRect frame = self.frame; 21 frame.origin.y = top; 22 self.frame = frame; 23 } 24 25 //bottom; 26 - (CGFloat)bottom{ 27 return CGRectGetMaxY(self.frame); 28 } 29 30 - (void)setBottom:(CGFloat)bottom{ 31 CGRect frame = self.frame; 32 frame.origin.y = [self bottom] - [self height]; 33 self.frame = frame; 34 } 35 36 //left; 37 - (CGFloat)left{ 38 return self.frame.origin.x; 39 } 40 41 - (void)setLeft:(CGFloat)left{ 42 CGRect frame = self.frame; 43 frame.origin.x = left; 44 self.frame = frame; 45 } 46 47 //right; 48 - (CGFloat)right{ 49 return CGRectGetMaxX(self.frame); 50 } 51 52 - (void)setRight:(CGFloat)right{ 53 CGRect frame = self.frame; 54 frame.origin.x = [self right] - [self width]; 55 self.frame = frame; 56 } 57 58 59 //座標,x,y 60 //x; 61 - (CGFloat)x{ 62 return self.frame.origin.x; 63 } 64 65 - (void)setX:(CGFloat)x{ 66 CGRect frame = self.frame; 67 frame.origin.x = x; 68 self.frame = frame; 69 } 70 71 //y; 72 73 - (CGFloat)y{ 74 return self.origin.y; 75 } 76 77 - (void)setY:(CGFloat)y{ 78 CGRect frame = self.frame; 79 frame.origin.y = y; 80 self.frame = frame; 81 } 82 83 //origin; 84 - (CGPoint)origin{ 85 return self.frame.origin; 86 } 87 88 - (void)setOrigin:(CGPoint)origin{ 89 CGRect frame = self.frame; 90 self.origin = origin; 91 self.frame = frame; 92 } 93 94 95 //中心點座標 centerX,centerY 96 //centerX; 97 - (CGFloat)centerX{ 98 return self.center.x; 99 }100 101 - (void)setCenterX:(CGFloat)centerX{102 CGPoint center = self.center;103 center.x = centerX;104 self.center = center;105 }106 107 108 //centerY;109 - (CGFloat)centerY{110 return self.center.y;111 }112 113 - (void)setCenterY:(CGFloat)centerY{114 CGPoint center = self.center;115 center.y = centerY;116 self.center = center;117 }118 119 120 //大小 ,121 //width;122 - (CGFloat)width{123 return self.frame.size.width;124 }125 126 - (void)setWidth:(CGFloat)width{127 CGRect frame = self.frame;128 frame.size.width = width;129 self.frame = frame;130 }131 132 133 //height;134 - (CGFloat)height{135 return self.frame.size.height;136 }137 138 - (void)setHeight:(CGFloat)height{139 CGRect frame = self.frame;140 frame.size.height = height;141 self.frame = frame;142 }143 144 //size;145 - (CGSize)size{146 return self.frame.size;147 }148 149 150 - (void)setSize:(CGSize)size{151 CGRect frame = self.frame;152 frame.size = size;153 self.frame = frame;154 }155 156 @endView Code

應用舉例:

1 //修改寬2 aview.width = 300;3 //修改x座標4 aview.x = 100;5 //修改y座標6 aview.y = 100;View Code

 

 

原文參考:http://mp.weixin.qq.com/s?__biz=MzA3NzM0NzkxMQ==&mid=216102953&idx=2&sn=703281ec344cc6fdb5b52f681002e255&scene=23&srcid=1018RAYFkM4iK97OMvC1c2PP#rd

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.