iOS界面自动旋转与自适应布局全解析
1. 指定旋转支持
在开发iOS应用时,需要指定应用支持的界面旋转方向。在代码中,有一个名为 shouldAutorotateToInterfaceOrientation:
的方法,它是iOS用来询问视图控制器是否可以旋转到特定方向的。以下是该方法的示例代码:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
iOS设备有四种常见的界面方向:
- UIInterfaceOrientationPortrait
- UIInterfaceOrientationPortraitUpsideDown
- UIInterfaceOrientationLandscapeLeft
- UIInterfaceOrientationLandscapeRight
对于iPhone项目,模板默认支持除倒置之外的所有方向;而对于iPad项目,模板创建的 shouldAutorotateTo