IOS 雜筆-12(類別de巧用 有便於Frame的操作),ios-12

來源:互聯網
上載者:User

IOS 雜筆-12(類別de巧用 有便於Frame的操作),ios-12

在實際開發中很多時候我們都為了控制項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

複製粘貼即可使用,也可以改變為其他控價。

相關文章

聯繫我們

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