iOS UIView 快速修改 frame

來源:互聯網
上載者:User

標籤:

我們修改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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.