美文网首页
竖屏应用里面添加横屏页面

竖屏应用里面添加横屏页面

作者: muice | 来源:发表于2018-05-11 21:03 被阅读17次

需求:
在某个竖屏应用里面有若干横屏。

处理方法
1 UINavagationController
由于当前的屏幕状态是通过UINavagationController来控制的,所以我们首先需要自定义一个BaseNavagationController:

// 这么做是为了当从横屏页面返回之后,能够让页面保持竖屏(其实是去获取返回之后VC的屏幕状态)
-(BOOL)shouldAutorotate
{
    return YES;
}

-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return [self.viewControllers.lastObject supportedInterfaceOrientations];
}

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [self.viewControllers.lastObject preferredInterfaceOrientationForPresentation];
}

2 UITabBarViewController
因为当前的应用是一个tabbar应用,所以我们也需要保证tabbar能够正常的显示。我使用了一个分类来处理这个问题,tabbarvc的状态依赖当前选中的VC的属性:

- (BOOL)shouldAutorotate
{
 return [self.selectedViewController shouldAutorotate];
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
 return [self.selectedViewController supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
 return [self.selectedViewController preferredInterfaceOrientationForPresentation];
}

3 BaseVC
然后在我的整个应用中,BaseVC是所有的VC的基类,通过这个我统一的处理了一些问题,因为当前的应用是竖屏的,所以在BaseVC中实现如下几个函数,保证VC竖屏显示:

// 只支持竖屏
- (BOOL)shouldAutorotate {
 return NO;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
 return UIInterfaceOrientationMaskPortrait;
}

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
 return UIInterfaceOrientationPortrait;
}

4 横屏VC的处理
我的横屏的VC也是继承了BaseVC,如果不处理,也会竖屏。通过以下代码进行特殊处理,覆盖BaseVC中的几个方法:

- (BOOL)shouldAutorotate {
 return YES;
}

- (UIInterfaceOrientationMask) supportedInterfaceOrientations {
 return UIInterfaceOrientationMaskLandscapeRight;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
 return UIInterfaceOrientationLandscapeRight;
}

这样就能让他正确的横屏显示,并且返回回去仍然是竖屏。

注意点
当在弹出横屏的VC的时候一定要注意以下几点:

当前的方式只支持模态弹出。push是会出问题的。

  1. APPDelegate需要添加下面代码
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
    return UIInterfaceOrientationMaskAll;
}

注意:听说要用BaseNavagationController进行包装才可以使用,否则会导致返回之后竖屏的页面变成横屏。但是我没有出现这个问题呀!

 UINavigationController *nav = [[BaseNavagationController alloc]initWithRootViewController:[[XXXViewController alloc] init]];
    [viewController presentViewController:nav animated:YES completion:completion];

相关文章

  • 竖屏应用里面添加横屏页面

    需求:在某个竖屏应用里面有若干横屏。 处理方法1 UINavagationController由于当前的屏幕状态是...

  • iOS 应用整体竖屏,单个页面横屏

    iOS 应用整体竖屏,单个页面横屏

  • iOS横屏开发

    一.指定某个页面横屏竖屏。 系统支持横屏顺序默认读取plist里面设置的方向(优先级最低)application设...

  • iOS 强制横屏和竖屏

    感谢这位同学的文章 需求:只有一个页面横屏而且是强制横屏,其他页面竖屏而且强制竖屏。 说明:本文方法只适用于pre...

  • 关于移动端H5横竖屏问题

    根据项目的一些需求,经常需要横屏展示H5,但对于开启自动横竖屏的手机,横屏时转竖屏,或者竖屏转横屏时,页面布局可能...

  • 横竖屏切换时Activity生命周期变化

    在Android10系统的Pixel上测试。横屏切换到竖屏: 竖屏切换到横屏: 配置文件添加了android:co...

  • iOS开发设置指定页面横屏显示,其余页面竖屏显示

    iOS开发设置指定页面横屏显示,其余页面竖屏显示 假设跳转逻辑为:从A页面跳转至B页面,B页面需要始终横屏显示,其...

  • iOS竖屏切到横屏

    1.AppDelegate 2、竖屏推到横屏(只横屏) 3、一个页面可横竖屏旋转 根据横竖屏更新UI

  • iOS 屏幕旋转shouldAutorotate

    最近项目中有个分时图的显示需要进行横屏处理。因为整个项目里面大部分页面都是需要竖屏显示的。只有几个页面是横屏显示。...

  • 横竖屏切换 (swift)

    一. 需求 APP中需要支持横屏和竖屏,并在不同的页面 可支持的屏幕旋转方向不一致 整体竖屏,部分强制横屏 整体横...

网友评论

      本文标题:竖屏应用里面添加横屏页面

      本文链接:https://www.haomeiwen.com/subject/nweqdftx.html