Autorotation and autosizing

Source: Internet
Author: User
Configure the application-level rotation direction-global setting

Method: click Project-General-deployment-device orientation.

It doesn't necessarily mean that every view in your application will use all of the selected orientations; but if you're going to support an orientation in any of your application's views, that orientation must be selected here.

 

Configure the view-level rotation direction-local setting

Method: viewcontroller. m

- (NSUInteger)supportedInterfaceOrientations{    return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft);}

Different views of the same app may have different rotation direction restrictions. You can only limit the orientation of the application level. For example, the application level does not support upside-down, and the view level does not.

Appendix, orientation of rotation:

  • Uiinterfaceorientationmaskportrait
  • Uiinterfaceorientationmasklandscapeleft
  • Uiinterfaceorientationmasklandscaperight
  • Uiinterfaceorientationmaskportraitupsidedown
  • Uiinterfaceorientationmasklandscape
  • Uiinterfaceorientationmaskall
  • Uiinterfaceorientationmaskallbutupsidedown

 

Rotate, same as Layout

Add labels with four corners (,)

Select the four labels (,) and select editor> resolve auto layout issues> Add missing constraints.

Add the intermediate (3, 4) label, alignment

Middle left (3) Editor-> align-> vertical center in Container

Middle right (4) Editor-> resolve auto layout issues-> Add missing Constraints

 

(1, 2) Add background color and adjust their width (random, not necessarily equal width)

 

(1, 2) Editor-> pin-> Horizontal spacing (regardless of how the rotation is not wide)


(1, 2) Editor-> pin-> widths equally (equal width no matter how it is rotated)

 

Rotate, landscape layout is different

Create a singleviewapplication and cancel autolayout in the file inspector of the view. We need to use the code to implement the layout.
Drag a view and four buttons in the main. storyboard and set the background color of the view.

Implementation Code in viewcontroller. m

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{    [self adjustLayout:toInterfaceOrientation];}

 

- (void)adjustLayout:(UIInterfaceOrientation)toInterfaceOrientation{    if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation))    {        [self layoutPortrait];    }    else    {        [self layoutLandscape];    }}

 

static const CGFloat btnHeight = 40;static const CGFloat btnWidth = 120;static const CGFloat spacing = 20;- (void)layoutPortrait{    CGRect b = self.view.bounds;    CGFloat contentHeight = CGRectGetHeight(b)-btnHeight*2-spacing*4;    CGFloat contentWidth = CGRectGetWidth(b) - spacing*2;    self.contentView.frame = CGRectMake(spacing, spacing, contentWidth, contentHeight);        CGFloat btn1x = spacing;    CGFloat btn2x = CGRectGetWidth(b)-spacing-btnWidth;    CGFloat btn1y = contentHeight+2*spacing;    CGFloat btn2y = btn1y+btnHeight+spacing;        self.btn1.frame = CGRectMake(btn1x, btn1y, btnWidth, btnHeight);    self.btn2.frame = CGRectMake(btn2x, btn1y, btnWidth, btnHeight);    self.btn3.frame = CGRectMake(btn1x, btn2y, btnWidth, btnHeight);    self.btn4.frame = CGRectMake(btn2x, btn2y, btnWidth, btnHeight);}- (void)layoutLandscape{    CGRect b = self.view.bounds;    CGFloat contentHeight = CGRectGetHeight(b)-spacing*2;    CGFloat contentWidth = CGRectGetWidth(b) - spacing*3-btnWidth;    self.contentView.frame = CGRectMake(spacing, spacing, contentWidth, contentHeight);        CGFloat btnx = CGRectGetWidth(b)-spacing-btnWidth;    CGFloat btny1 = spacing;    CGFloat btny4 = CGRectGetHeight(b)-spacing-btnHeight;    CGFloat btny2 = btny1+(btny4-btny1)/3.0;    CGFloat btny3 = btny2+(btny4-btny1)/3.0;        self.btn1.frame = CGRectMake(btnx, btny1, btnWidth, btnHeight);    self.btn2.frame = CGRectMake(btnx, btny2, btnWidth, btnHeight);    self.btn3.frame = CGRectMake(btnx, btny3, btnWidth, btnHeight);    self.btn4.frame = CGRectMake(btnx, btny4, btnWidth, btnHeight);}
View code

 

- (void)viewDidLoad{    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.    UIApplication *app = [UIApplication sharedApplication];    UIInterfaceOrientation currentOrientation = app.statusBarOrientation;    [self adjustLayout:currentOrientation];}

 

Effect

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.