UIViewController屏幕旋转
强制旋转为横屏代码:
//这句话是防止手动先把设备置为横屏,导致第二行代码失效.
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationPortrait] forKey:@"orientation"];
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationLandscapeLeft] forKey:@"orientation"];
强制旋转为竖屏代码:
//这句话是防止手动先把设备置为横屏,导致第二行代码失效.
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationLandscapeLeft] forKey:@"orientation"];
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationPortrait] forKey:@"orientation"];
UIViewController支持屏幕旋转需要重写以下两个方法:
// 支持设备自动旋转
- (BOOL)shouldAutorotate{
return YES;
}
// 支持横竖屏显示
- (NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskAll;
}
注意:屏幕所支持的方向还要在TARGETS–General–Device Orientation中根据情况勾选(Portrait、UpsideDown、LandscapeLeft、LandscapeRight)。
Device Orientation与supportedInterfaceOrientations方法返回值的对应关系:
Device Orientation | supportedInterfaceOrientations方法返回值 |
---|---|
LandscapeLeft | UIInterfaceOrientationMaskLandscapeRight |
LandscapeRight | UIInterfaceOrientationMaskLandscapeLeft |