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