屏幕旋转
2019-12-12 本文已影响0人
傲骨天成科技
本文涉及到的转屏是咱们的app的某个页面设置横竖屏的切换
- 必须先在appdelegate中实现下面的方法
-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { return UIInterfaceOrientationMaskLandscape | UIInterfaceOrientationMaskPortrait; }
-
在对应的ViewController中重写以下三个方法:
- 支持屏幕旋转
- (BOOL) shouldAutorotate { retunrn YES; }
2.当前viewController支持的设备方向
-(UIInterfaceOrientationMask)supportedInterfaceOrientations { if (self.a == 0) { return UIInterfaceOrientationMaskPortrait; }else { return UIInterfaceOrientationMaskLandscape; } }
其中的a是当前VC的一个全局属性,用于你控制旋转屏幕的时候支持的屏幕方向。
- 默认进入到当前的VC的时候显示的方向
- 支持屏幕旋转
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
return UIInterfaceOrientationPortrait;
}
- 状态栏是否隐藏
- (BOOL)prefersStatusBarHidden {
return NO; // your own visibility code
}
5.点击某个按钮进行横屏
- (void)switchToLandscape
{
self.a = 1;
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationLandscapeRight] forKey:@"orientation"];
[UIViewController attemptRotationToDeviceOrientation];
}
6.点击某个按钮进行竖屏
- (void)switchToPortrait
{
self.a = 0;
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationPortrait] forKey:@"orientation"];
[UIViewController attemptRotationToDeviceOrientation];
}