IOS miscellaneous pen-11 (the size of UIView cannot be changed externally), iosuiview

Source: Internet
Author: User

IOS miscellaneous pen-11 (the size of UIView cannot be changed externally), iosuiview

I may not be very clear about the question. Now I will introduce this article in detail.

The size of the UIVIew control cannot be changed externally.

This is UIView, but in fact, most controls are not only UIView.

What should I do if the size cannot be changed externally.

First, rewrite setFrame to set its own size, as shown below:

//// TestView. m // CX-the Size of the UIView cannot be changed externally. // 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

After rewriting setFrame, we can perform a test.

In VC, set the size of TestVIew to {200,200 }.

Therefore, the Size of UITestView cannot be changed externally.

However, the following results are not true.

We first set the UITestView Center.

Then set the Bounds of UITestView

/// ViewController. m // CX-the Size of the UIView cannot be changed externally. // 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

The result is as follows:

It can be seen that the size of UITestView has changed. It doesn't matter.

Let's rewrite bounds.

//// TestView. m // CX-the Size of the UIView cannot be changed externally. // 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

The result is as follows:

//// TestView. m // CX-the Size of the UIView cannot be changed externally. // 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

We can conclude that if the size cannot be changed externally by using the UIView control, we only need to override frame and bounds.

Similarly, we can implement some other operations.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.