IOS 雜筆-11(實現在外部無法改變UIView的size),iosuiview

來源:互聯網
上載者:User

IOS 雜筆-11(實現在外部無法改變UIView的size),iosuiview

我想題目說的或許不是很清楚,那麼現在我詳細介紹一下這篇隨筆內容。

在外部無法改變UIVIew控制項的size。

這裡說是UIView,但是事實上,是大多數控制項而絕非僅UIView。

想要實現在外部無法改變size該怎麼做呢。

首先是重寫setFrame使其規定本身size,如下

////  TestView.m//  CX-實現在外部無法改變UIView的Size////  Created by ma c on 16/3/25.//  Copyright © 2016年 xubaoaichiyu. All rights reserved.//#import "TestView.h"@implementation TestView-(void)setFrame:(CGRect)frame{        frame.size = CGSizeMake(100, 100);        [super setFrame:frame];}@end

重寫setFrame後我們可以進行測試。

在VC裡我吧TestVIew的size 設定為{200,200}。

由此可見,在外部無法改變UITestView的Size

但是下面的結果卻並非如此

我們先是設定UITestView的Center。

然後設定UITestView的Bounds

////  ViewController.m//  CX-實現在外部無法改變UIView的Size////  Created by ma c on 16/3/25.//  Copyright © 2016年 xubaoaichiyu. All rights reserved.//#import "ViewController.h"#import "TestView.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];            TestView * view = [[TestView alloc]init];        view.center = self.view.center;        view.bounds = CGRectMake(0, 0, 200, 200);        [self.view addSubview:view];        NSLog(@"%@",NSStringFromCGRect(view.frame));    }@end

結果如下

可見:UITestView 的size有所改變,沒關係。

我們再重寫一下bounds。

////  TestView.m//  CX-實現在外部無法改變UIView的Size////  Created by ma c on 16/3/25.//  Copyright © 2016年 xubaoaichiyu. All rights reserved.//#import "TestView.h"@implementation TestView-(void)setFrame:(CGRect)frame{        frame.size = CGSizeMake(100, 100);        [super setFrame:frame];}-(void)setBounds:(CGRect)bounds{        bounds.size = CGSizeMake(100, 100);        [super setBounds:bounds];}@end

結果如下:

////  TestView.m//  CX-實現在外部無法改變UIView的Size////  Created by ma c on 16/3/25.//  Copyright © 2016年 xubaoaichiyu. All rights reserved.//#import "TestView.h"@implementation TestView-(void)setFrame:(CGRect)frame{        frame.size = CGSizeMake(100, 100);        [super setFrame:frame];}-(void)setBounds:(CGRect)bounds{        bounds.size = CGSizeMake(100, 100);        [super setBounds:bounds];}@end

由此得出結論,如果想要是UIView控制項在外部無法改變size,我們只需要重寫frame,bounds即可。

同理,我們還可以實現一些其他的操作。

相關文章

聯繫我們

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