iPad UISplitViewController 启动方向 - 根视图纵向显示

发布于 2024-09-07 06:17:42 字数 934 浏览 3 评论 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,应该有更好的方法,但我一生都找不到它。

有什么想法吗?

So I have a UISplitViewController that is the root view of my program. I have set the initial orientation to be LandscapeLeft and have all 4 orientations supported in my plist.

If I launch the app in portrait - the view that is shown is my root view not the detail view which obviously is not what I want. As soons as I rotate the device, everything works from there.

I have scoured this site and others but have found no fixes. The closest I have come is the adding the following to my app delegate:

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

So this essentially forces it to draw correctly, which works great except for when the app launches with the device laying flat. Then I dont know the portrait vs landscape (and I know of no way to find it). So I basically then cant accurately say what to do.

The larger thing is this above code is a HACK, and there should be a better way, but for the life of me I cant find it.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

女中豪杰 2024-09-14 06:17:42

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

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

}

在我的应用程序中,这有效,因为我只是想强制首先将它们设置为纵向,如果之后转向横向,它会旋转并且工作正常,因为我支持所有方向。

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

希望有帮助...

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

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

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

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

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

        }

当应用程序启动时,这在应用程序委托中不起作用,因为这个detailViewController那时还没有被使用。只是想让你知道是否有帮助。

Thanks for your code on fixing this for portrait and landscape. I found a quick fix for the issue when its flat on the desk or upside down(not sure who would start the app upside down, but the same issue happens in this case):

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

}

In my App, this works because I just wanted to force them into portrait to start and if they move to landscape after that, it rotates and works fine because I support all orientations.

I don't think this will work in all cases, but this might help you get on the right track. You just need to play with the statusBarOrientation value and manually set it based on what you find works if the InterfaceOrientation is not valid(which it isn't if the device is flat on a table or upside down).

Hope that helps...

Edit: Also, I have a need in my App for knowing orientation while they are picking new items in the SplitViewController, so it was being wonky when the device was flat or upside down. And the way I detected if it was flat or upside down was to first see if the orientation was valid like you did:

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

But then if its not, I check to see if self.popoverController is nil. If its not, then its in portrait and if it is, then its landscape (based on how the splitViewController works by setting that to nil or not):

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

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

        }

This wouldn't work in the App Delegate while the App is launching though because this detailViewController isn't being used at that point. Just thought I'd let you know if it helps out any.

疧_╮線 2024-09-14 06:17:42

您的 RootViewController 正在显示而不是您的DetailViewCOntroller?
看来你做了一些坏事......(也许 SplitViewController.viewControllers 中的 viewControllers 顺序颠倒了?)

Your RootViewController is showing in place of your detailViewCOntroller?
it seems you're doing something bad... (maybe inverted viewControllers order in SplitViewController.viewControllers ?)

月光色 2024-09-14 06:17:42

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

You need to make sure to add your UISplitViewController's view to the UIWindow INSIDE the application:application didFinishLaunchingWithOptions: method of your app delegate (not later), or the split view won't rotate correctly initially.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文