屏幕旋轉(zhuǎn)控制分動態(tài)旋轉(zhuǎn)和手動代碼控制旋轉(zhuǎn)乔遮,多見于視頻播放。
一 .APP設(shè)置
-
在配置中關(guān)閉屏幕方向。
info.plist 在
AppDelegate
添加如下方法 返回所需要的旋轉(zhuǎn)方向坎匿。代碼設(shè)置的方向優(yōu)先級將大于info.plist
中的設(shè)置。
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
return UIInterfaceOrientationMaskAllButUpsideDown;
}
-
UITabBarController
方向由選中的controller
的方向控制,在UITabBarController
中添加如下設(shè)置:
- (BOOL)shouldAutorotate
{
return [self.selectedViewController shouldAutorotate];
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return [self.selectedViewController supportedInterfaceOrientations];
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [self.selectedViewController preferredInterfaceOrientationForPresentation];;
}
- 在
UINavigationController
中由topViewController
方向決定
- (BOOL)shouldAutorotate {
return [self.topViewController shouldAutorotate];
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return [self.topViewController supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return [self.topViewController preferredInterfaceOrientationForPresentation];
}
二. 旋轉(zhuǎn)控制
- 在需要動態(tài)控制旋轉(zhuǎn)的
controller
配置:
//是否支持自動轉(zhuǎn)屏
- (BOOL)shouldAutorotate
{
return YES;
}
// 支持哪些屏幕方向
-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAllButUpsideDown;
}
// 默認(rèn)的屏幕方向(當(dāng)前ViewController必須是通過模態(tài)出來的UIViewController(模態(tài)帶導(dǎo)航的無效)方式展現(xiàn)出來的雷激,才會調(diào)用這個方法)
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
- 手動控制旋轉(zhuǎn)
/// 旋轉(zhuǎn)
/// @param aUIDeviceOrientation 需要設(shè)置的旋轉(zhuǎn)方向
-(void)changeWithDeviceOrientation:(UIDeviceOrientation)aUIDeviceOrientation{
UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];
if (aUIDeviceOrientation != deviceOrientation) {
if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
[[UIDevice currentDevice] setValue:@(aUIDeviceOrientation) forKey:@"orientation"];
}
}
}