【问题标题】:iOS keep status bar in portraitiOS保持纵向状态栏
【发布时间】:2014-03-07 10:47:49
【问题描述】:

我有一个使用接近传感器的应用,但接近传感器在横向模式下不起作用。我听说如果您将状态栏保持在纵向模式,传感器就会工作 我已经尝试过了,但它没有用。

[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];

状态栏仍切换为横向模式。

我该怎么办?

【问题讨论】:

    标签: ios


    【解决方案1】:

    如果您想让您的应用程序保持纵向模式而不考虑 DeviceOrientation,我建议您将以下代码添加到您的 viewController。

    - (BOOL) shouldAutorotate{
        return NO;
    }
    - (NSUInteger) supportedInterfaceOrientations{
        return UIInterfaceOrientationMaskPortrait;
    }
    - (UIInterfaceOrientation) preferredInterfaceOrientationForPresentation{
        return UIInterfaceOrientationPortrait;
    }
    

    这将确保您的应用以纵向模式启动,并保持纵向模式,即使设备已转动。

    编辑:为了在纵向模式下只有一个视图时保持应用程序处于横向状态,请将上述代码添加到您希望限制为纵向模式的一个 viewController。

    将以下函数添加到您的 appDelegate:

    - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
        /* This is in order to allow views that do not support landscape mode to be able to load in
         */
        return UIInterfaceOrientationMaskAll;
    }
    

    将以下代码添加到您的其他视图控制器:

    - (BOOL) shouldAutorotate{
            return YES;
        }
        - (NSUInteger) supportedInterfaceOrientations{
            return UIInterfaceOrientationMaskLandscape;
        }
        - (UIInterfaceOrientation) preferredInterfaceOrientationForPresentation{
            return UIInterfaceOrientationLandscapeRight; //you can choose either landscape orientation here
        }
    

    我建议你使用这个层次结构:

    • RootViewController - 它将包含引用(最好 强)到您的接近传感器,我建议您在 后台线程。这将仅支持纵向模式,因此您的 接近传感器可以正常工作(我希望)。
    • displayViewController - 它将包含您拥有的所有 UI 元素,并且仅支持横向模式
    • 您可以使用委托管理它们之间的通信

    另外,在 RootViewController 的 viewDidApper 之后,请使用 presentViewController (iOS 6.0 及以上) 或 presentModalViewController (iOS 5.x) 选择器在屏幕上添加 displayViewController 的视图。使用[self.view addSubview: displayViewController.view]; 是行不通的(我是根据个人经验说的)。

    【讨论】:

    • 这当然是假设您使用的是iOS 6.0或更高版本
    • 谢谢,但我的应用是为横向模式构建的,所以如果它处于纵向模式,那么它会搞砸一切。
    • 如果我使用它,该视图会完全处于纵向模式。因为应用程序的所有视图都是为横向设计的,如果视图是纵向的,那很好,但我不希望视图中的对象移动。
    • 如果你使用presentViewController(iOS 6.0及以上)应该没问题。您的 rootViewController 将始终保持纵向,但您的 displayViewController 将响应横向左侧和横向右侧。请务必设置一个委托,以便可以在两个视图控制器之间传输接近传感器信息和 UI 事件。 (PS:我的回答出错了,是presentViewController,不是pushViewController——已经编辑过了)
    • 所以我视图上的对象不会以任何方式移动或改变?
    猜你喜欢
    • 2017-11-19
    • 1970-01-01
    • 1970-01-01
    • 2011-12-17
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多