设置横屏很简单,只需要勾选上这两项就可以了
但这是设置全局的横屏允许,很多时候需求是大多界面只允许竖屏,只有某个界面才需要设置横屏(例如播放视频界面等),所以这时这两项就不能勾选上,那要怎么设置横屏呢,其实很简单,当然本文的方法只适用present视图
首先在appdelegate中添加一个属性
@property (nonatomic,assign) BOOL allowRotation;
然后添加代理方法
-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if (self.allowRotation) {
return UIInterfaceOrientationMaskLandscape;
}
return UIInterfaceOrientationMaskPortrait;
}
在需要设置横屏的视图present前设置为YES
AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
delegate.allowRotation = YES;
在横屏视图dismiss前设置为NO即可恢复竖屏
随意转换横竖屏(present 视频播放时全屏转换)
//修改全局属性并调用转屏方法
AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
appDelegate.allowRotation = NO;
[self setNewOrientation:NO];//调用转屏代码
//强制转屏
- (void)setNewOrientation:(BOOL)fullscreen
{
if (fullscreen) {
NSNumber *resetOrientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];
[[UIDevice currentDevice] setValue:resetOrientationTarget forKey:@"orientation"];
NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight];//横屏方向(根据需求选填)
[[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
}else{
NSNumber *resetOrientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];
[[UIDevice currentDevice] setValue:resetOrientationTarget forKey:@"orientation"];
NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
[[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
}
}