Layoutsubviews when to invoke an issue

Source: Internet
Author: User

This article reprinted to http://www.cnblogs.com/pengyingh/articles/2417211.html

Today with Vencay brother learned a UIView Setneedsdisplay and Setneedslayout method. The first two methods are executed asynchronously. And Setneedsdisplay will call the automatic call DrawRect method, so you can get uigraphicsgetcurrentcontext, you can draw. Setneedslayout, by default, calls Layoutsubviews to handle some of the data in the child view.
The pope, Setneedsdisplay convenient drawing, and layoutsubviews convenient data.
\

ipad Screen Switching solution
August 01, 2011 Monday 10:09
Because the ipad's screen is different, so good app, the layout of the screen is not the same. Then you need the overall solution of the screen. First look at a different screen layout of the interface.

The above two graphs are a screenshot from the same interface. As you can see, the content is displayed in the same format, but the interface layout is different. To achieve the above layout, the main use of UIView in the Layoutsubviews method. When UIView is set to automatically adapt the screen, when the user rotates the device, the Layoutsubviews method is called, and we simply override this method and then determine the orientation of the user's screen. You can adjust the position of each space.

The following is the simplest prototype for implementing the above interface:

The first analysis can know that the left is a picture, the right is a picture plus text view. The following is the implementation of a left view to the right is a picture plus a paragraph of the case.
Examples are as follows:

The text on the right and the green part are encapsulated with a sub-view.
The whole layout is I added a contentview view in the main view and a Articleview view in the Contentview.
where Articleview and Contentview's xib files are open.


Override the Layoutsubviews method in Contentview and then judge the vertical and horizontal screen of the current view according to the direction of the Stausbar. Specific code:
-(void) layoutsubviews{
[Super Layoutsubviews];
Uideviceorientation interfaceorientation=[[uiapplication sharedapplication] statusbarorientation];
if (interfaceorientation = = Uideviceorientationportrait | | interfaceorientation = = Uideviceorientationportraitupsidedown) {
When flipping to a vertical screen
[Self setverticalframe];
}else if (interfaceorientation==uideviceorientationlandscapeleft | | interfaceorientation = = Uideviceorientationlandscaperight) {
When flipping to a 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 specific screen method, the new set of the coordinates of each component.

Next, add the Articleview view in Contentview.
-(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 am using Xib, the initialization method is Initwithcoder, and a new view is added to this.

Also set the coordinates of the corresponding space in the Articleview.
-(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 flipping to a vertical screen
[Self setverticalframe];
}else if (interfaceorientation==uideviceorientationlandscapeleft | | interfaceorientation = = Uideviceorientationlandscaperight) {
When flipping to a 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)];
}

Layoutsubviews when to invoke an issue

Layoutsubviews when to call the problem, this method is when you need to adjust the size of the subview need to rewrite (I this translation is not rigorous, the following is the original: You should override this method only if the Autoresizing behaviors of the subviews does not offer the behavior to you want.), but sometimes it is often expected to be called when it is not called, and does not want it to be called when it is called, making it very angry. According to the overseas community One person post, has done the summary translation.

The layoutsubviews is called in the following cases:

1. Init initialization does not trigger Layoutsubviews
2, Addsubview will trigger Layoutsubviews
3, set the view frame will trigger the layoutsubviews, of course, if the frame value has changed before and after the setting
4, rolling a uiscrollview will trigger layoutsubviews
5. Rotate screen will trigger the Layoutsubviews event on the parent UIView
6, change the size of a uiview will also trigger the parent UIView on the Layoutsubviews event

Layoutsubviews when to invoke an issue

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.