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 |
本文详细介绍了如何使用Objective-C代码强制UIViewController进行屏幕旋转,包括横屏和竖屏的实现方式,以及需要重写的两个关键方法:shouldAutorotate和supportedInterfaceOrientations。同时,文章还提到了设置设备方向的注意事项。
1826

被折叠的 条评论
为什么被折叠?



