IOS: autosizing

Source: Internet
Author: User

If the app supports rotation, it will almost certainly involve the autosizing problem of uiview.

There are two methods for autosize:

First, set it in the Nb size inspectator property panel.

First, use code to set the autoresizingmask attribute of uiview

Note: For margin, the settings in Nb are logically different from those in code settings.

Example:

The position of the uiview in the upper left corner of the screen (top & left) remains unchanged,

If you want code, it should be

Subview. autoresizingmask = uiviewautoresizingflexiblebottommargin | uiviewautoresizingflexiblerightmargin;

The corresponding Nb settings are:

To use uiview, the upper, lower, and left margin values are flexable (that is, when rotate is used, the upper, lower, and left margin values must be adjusted)

If you want code, it should be

Subview. autoresizingmask = uiviewautoresizingflexiblebottommargin | uiviewautoresizingflexibletopmargin | contents | uiviewautoresizingflexiblerightmargin;

The corresponding Nb settings are:

For uiview, the upper, lower, and left margin are fixed. (If you want uiview to rotate without autosizing, that is, manually write code to set the position and size, you should use this)

If you want code, it should be

Subview. autoresizingmask = uiviewautoresizingnone;

The corresponding Nb settings are:

With the above settings, you may also need to manually write code to set the location and size of the uiview, Example

View1.frame = cgrectmake (10, 10,
100,100 );

We recommend that you write the code for setting the uiview frame in willanimaterotationtointerfaceorientation, while the shouldautorotatetointerfaceorientation method is used to set the supported rotation directions.

Note: willanimaterotationtointerfaceorientation will not be called when the app is started. It will only be called after rotation, and shouldautorotatetointerfaceorientation will also be called at startup and will be called more than once!

Example:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{    NSLog(@"shouldAutorotateToInterfaceOrientation");    return YES;}- (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{    NSLog(@"willAnimateRotationToInterfaceOrientation");        if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft ||        toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)    {        view1.frame=CGRectMake(10, 10, 100, 100);    }    else    {        view1.frame=CGRectMake(50, 50, 50, 50);    }    }

Ref links:

Http://www.cnblogs.com/xingchen/archive/2012/02/29/2374798.html

Http://www.techotopia.com/index.php/IOS_4_iPhone_Rotation,_View_Resizing_and_Layout_Handling

Http://blog.csdn.net/yuquan0821/article/details/7596545

Http://www.cocoachina.com/bbs/read.php? Tid = 98457

Http://hi.baidu.com/rslhg/blog/item/c5338dbf03fa701318d81f64.html

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.