0

所以我有一个 UISplitViewController,它是我程序的根视图。我已将初始方向设置为 LandscapeLeft,并在我的 plist 中支持所有 4 个方向。

如果我以纵向方式启动应用程序 - 显示的视图是我的根视图而不是详细视图,这显然不是我想要的。只要我旋转设备,一切都会从那里开始。

我已经搜索了这个网站和其他网站,但没有找到任何修复方法。我最接近的是将以下内容添加到我的应用程序委托中:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

if (UIDeviceOrientationIsValidInterfaceOrientation([UIDevice currentDevice].orientation)) {
    if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))
        [UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationLandscapeLeft;
    else
        [UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationPortrait;
}

[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];

因此,这实质上会强制它正确绘制,除了当应用程序在设备平放的情况下启动时效果很好。然后我不知道纵向与横向(而且我不知道找不到它)。所以我基本上不能准确地说该怎么做。

更大的事情是上面的代码是一个HACK,应该有更好的方法,但对于我的生活我找不到它。

有任何想法吗?

4

3 回答 3

1

感谢您为纵向和横向修复此问题的代码。当它平放在桌子上或倒置时,我找到了一个快速解决问题的方法(不确定谁会倒置启动应用程序,但在这种情况下会发生同样的问题):

if (UIDeviceOrientationIsValidInterfaceOrientation([UIDevice currentDevice].orientation)) {
    if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))
        [UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationLandscapeLeft;
    else if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
        [UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationPortrait;


}
else {

    if (([UIApplication sharedApplication].statusBarOrientation == 1) || ([UIApplication sharedApplication].statusBarOrientation == 0))
    {
        [UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationPortrait;
    }
    else {
        [UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationLandscapeLeft;
    }

    NSLog(@"orientation: %d", [UIApplication sharedApplication].statusBarOrientation);

}

在我的应用程序中,这是可行的,因为我只想强制它们进入纵向开始,如果之后它们移动到横向,它会旋转并且工作正常,因为我支持所有方向。

我认为这并不适用于所有情况,但这可能会帮助您走上正轨。如果 InterfaceOrientation 无效(如果设备平放在桌子上或倒置,则不是),您只需要使用 statusBarOrientation 值并根据您发现的工作手动设置它。

希望有帮助...

编辑:另外,当他们在 SplitViewController 中挑选新项目时,我需要在我的应用程序中了解方向,因此当设备平放或倒置时它会很不稳定。我检测它是平的还是倒置的方法是首先看看方向是否像你一样有效:

if (UIDeviceOrientationIsValidInterfaceOrientation([UIDevice currentDevice].orientation))

但如果不是,我会检查 self.popoverController 是否为零。如果不是,那么它是纵向的,如果是,那么它的横向(基于 splitViewController 的工作方式,将其设置为 nil 与否):

if (self.popoverController)
        {
            NSLog(@"in portrait");

        }
        else {
            NSLog(@"in landscape");

        }

这在 App 启动时在 App Delegate 中不起作用,因为此时没有使用这个 detailViewController。只是想我会告诉你是否有帮助。

于 2010-11-03T19:25:38.653 回答
0

您的 RootViewController 代替您的 detailViewCONtroller 显示?看来你做错了什么......(可能是在 SplitViewController.viewControllers 中颠倒了 viewControllers 的顺序?)

于 2010-07-06T14:36:34.360 回答
0

您需要确保将 UISplitViewController 的视图添加到application:application didFinishLaunchingWithOptions:应用程序委托的方法内的 UIWindow(而不是稍后),否则拆分视图最初不会正确旋转。

于 2010-10-12T22:35:51.330 回答