Explain the Layoutsubviews layout method of UIView in IOS using _ios

Source: Internet
Author: User

Concept
in UIView there is a method layoutsubviews:

Copy Code code as follows:

-(void) layoutsubviews; Override Point. Called by layoutifneeded automatically. As of IOS 6.0, when constraints-based layout is used the base implementation applies the constraints-based layout, Otherwi Se it does nothing.

The specific function of Layoutsubviews method
Layoutsubviews is the subviews of the new layout. For example, when we want to update the location of a child view, we can do this by calling the Layoutsubviews method, which can be used to rearrange the sub view.
Layoutsubviews default is not to do anything, when used, you need to rewrite from the mine.

Instance
because the ipad's screen is different, so good application, the page layout of the same screen is not the same. Then you need the overall solution of the screen. First look at a horizontal screen layout is not the same interface.
The following two pictures are from the same interface of the horizontal screen screenshot. As you can see, the overall display of the same content, but the interface layout is different. To achieve the above layout, the main use of UIView Layoutsubviews method. When the UIView is set to the Auto Fit screen, when the user rotates the device, the Layoutsubviews method is invoked, and we simply rewrite the method and then judge the direction of the user's screen. You can adjust the position of each space.

Here is the simplest prototype for implementing the above interface:
First analysis can know the left is a picture, right is a picture plus text view. The following is the implementation of a left view the right is a picture plus a word case.
The screenshot of the case is as follows

The text and the green part of the right are encapsulated in a child view.
The whole layout is that I added a contentview view to the main view and a Articleview view in the Contentview.
where Articleview and Contentview's xib files are open.

Rewrite the Layoutsubviews method in Contentview, and then determine the vertical screen of the current view according to the direction of the Stausbar. Specific code:

Copy Code code as follows:

-(void) layoutsubviews{
[Super Layoutsubviews];
Uideviceorientation interfaceorientation=[[uiapplication sharedapplication] statusbarorientation];
if (interfaceorientation = = Uideviceorientationportrait | | interfaceorientation = = Uideviceorientationportraitupsidedown) {
When flipped to the vertical screen
[Self setverticalframe];
}else if (interfaceorientation==uideviceorientationlandscapeleft | | interfaceorientation = = Uideviceorientationlandscaperight) {
When flipped to horizontal screen
[Self sethorizontalframe];
}
}

-(void) setverticalframe
{
NSLog (@ "vertical screen");
[Titlelable setframe:cgrectmake (283, 0, 239, 83)];
[Leftview Setframe:cgrectmake (38, 102, 384, 272)];
[Rightview setframe:cgrectmake (450, 102, 282, 198)];
}

-(void) sethorizontalframe
{
NSLog (@ "horizontal screen");
[Titlelable setframe:cgrectmake (183, 0, 239, 83)];
[Leftview Setframe:cgrectmake (168, 122, 384, 272)];
[Rightview setframe:cgrectmake (650, 122, 282, 198)];
}


In the concrete screen method, the coordinates of each component can be reset.

Next, add the Articleview view to the Contentview.

Copy Code code as follows:

-(ID) Initwithcoder: (Nscoder *) Adecoder
{
if (self = [super Initwithcoder:adecoder])) {

Nsarray *arraycontentview =[[nsbundle Mainbundle] loadnibnamed:@ "Articleview" owner:self Options:nil];
Rightview=[arraycontentview objectatindex:0];
[Self addsubview:rightview];
}
return self;
}


Since I'm using xib, the initialization method is Initwithcoder, and a new view is added to this.

Also in the Articleview set the coordinates of the corresponding space in the horizontal screen.

Copy Code code as follows:

-(void) layoutsubviews{
[Super Layoutsubviews];
Uideviceorientation interfaceorientation=[[uiapplication sharedapplication] statusbarorientation];
CGRect Rect=self.frame;
rect.size.width=282;
rect.size.height=198;
[Self setframe:rect];
if (interfaceorientation = = Uideviceorientationportrait | | interfaceorientation = = Uideviceorientationportraitupsidedown) {
When flipped to the vertical screen
[Self setverticalframe];
}else if (interfaceorientation==uideviceorientationlandscapeleft | | interfaceorientation = = Uideviceorientationlandscaperight) {
When flipped to horizontal screen
[Self sethorizontalframe];
}
}

-(void) setverticalframe
{
NSLog (@ "vertical screen");
[Contentview Setframe:cgrectmake (12, 6, 250, 125)];
[Textlable Setframe:cgrectmake (50, 139, 182, 39)];
}

-(void) sethorizontalframe
{
NSLog (@ "horizontal screen");
[Contentview Setframe:cgrectmake (12, 6, 106, 158)];
[Textlable Setframe:cgrectmake (135, 11, 147, 39)];
}

Summarize
Layoutsubviews The following conditions will be invoked:

Apple's official document has emphasized that it is not possible to directly call the Layoutsubviews child view for a new layout. So, what would layoutsubviews be called? Through Baidu Search, found that the following several situations layoutsubviews will be called.

    • Call Setlayoutsubviews directly. (This is stated in the Apple Official document above)
    • When the Addsubview.
    • When the view frame changes.
    • When you slide the Uiscrollview.
    • Rotating screen triggers the Layoutsubviews event on the parent UIView.
    • Changing the size of a uiview also triggers the Layoutsubviews event on the parent UIView.

I have a simple test, the above will be called basically. Attention:

When the value of the view's Fram is 0, ' addsubview ' does not call ' layoutsubviews '.
The Layoutsubviews method is handy when you are laying out your own view of the mine. You can make a thorough understanding of Layoutsubviews's calling mechanism.

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.