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.