iOS 单个页面设置横屏

本文详细介绍了在iOS开发中如何设置页面为横屏显示,并提供了代码示例。包括如何通过设置preferredInterfaceOrientationForPresentation和supportedInterfaceOrientations实现页面默认横屏,以及如何通过代码手动调整屏幕方向。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

 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];

}

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

卡卡西Sensei

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值