ios 单个页面设置横屏

2,344 阅读1分钟

我们在开发中,有些时候需要设置横屏浏览,特别是视频播放页面是经常用的到横屏,也是横屏和竖屏之间来回的切换运用最多的。

那么我们应该怎么设置和适配横屏呢???

 1.进入页面默认横屏效果

  1. 设置页面屏幕屏幕方向

  2. 设置页面屏幕支持的方向

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationLandscapeRight;
}//页面需要的横屏方向
 
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscapeRight;
}//页面支持屏幕横屏的方向

加入 B 页面这样设置,A 页面进入到 B 页面,B 页面就是横屏的了,不过需要注意的是,只有

[self presentViewController:vc animated:YES completion:nil] 这样进入 B页面才会生效,push 进入的不会生效

2.代码手动设置横屏

1,设置页面屏幕屏幕方向 2,设置页面屏幕支持的方向 3,手动调用

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationLandscapeRight;
}//页面需要的横屏方向
 
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscapeRight;
}//页面支持屏幕横屏的方向
 
// 改变页面屏幕方向的方法
- (void)forceToOrientation:(UIDeviceOrientation)orientation
{
    NSNumber *orientationUnknown = [NSNumber numberWithInt:0];
    [[UIDevice currentDevice] setValue:orientationUnknown forKey:@"orientation"];
 
    NSNumber *orientationTarget = [NSNumber numberWithInt:orientation];
    [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
}

点击按钮调用设置屏幕方向

- (void)changeUIDeviceOrientation:(UIButton *)sender {
   [self forceToOrientation:UIDeviceOrientationLandscapeRight];
}

在 viewDidLoad  里面设置也可以进入页面自动横屏( push)

- (void)viewDidLoad {
    [super viewDidLoad];
 
	[self forceToOrientation:UIDeviceOrientationLandscapeRight];
 
}