iPhone 屏幕随机旋转?

发布于 2024-08-25 10:28:16 字数 395 浏览 4 评论 0原文

我使用 tabBar 控制器作为根控制器。它有 4 个选项卡,每个 ViewController

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
        return YES;
}

以及 tabBarController 本身都有。 但是当我旋转设备(真实的或模拟器)时,屏幕随机转动!如果我打开应用程序时它没有转动,它将具有相同的行为,直到我退出应用程序。 我尝试在IB中一一添加4个viewController,看看其中一个是否有问题,但我遇到了同样的问题。只有在根本没有标签的情况下它才会始终转动!

如果您有任何想法,请告诉我。谢谢!

I use a tabBar Controller as root controller. It has 4 tabs and each of its ViewControllers has

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
        return YES;
}

as well as the tabBarController itself.
But when I rotate the device (real or simulator), the screen turns at random! If it doesn't turn when I open the application it would have the same behavior until I quit the app.
I tried to add the 4 viewControllers one by one in IB to see if one was problematic, but I obtained the same issue. It only always turns when there is no tabs at all!

Please tell me if you have any ideas. Thanks!

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

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

发布评论

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

评论(4

睫毛溺水了 2024-09-01 10:28:16

您将每个视图控制器设置为响应任何可能的方向。因此,每个视图都会尝试旋转到每个方向。

视图不会真正自动旋转。除了最简单的视图之外,您通常必须以编程方式管理子视图的放置。

如果您没有自定义方向代码,您可能会看到视图尝试在横向框架中绘制纵向视图,反之亦然。如果您设置了自动调整子视图大小,您的子视图将以看似随机的模式分散在屏幕上。改变方向的次数越多,放置的位置就越随机。

对于复杂的视图,我喜欢为每个方向创建单独的 viewController/view 对。然后我将视图放入导航控制器中。当方向改变时,每个视图控制器将针对即将到来的方向将适当的视图控制器推入或弹出堆栈。对于用户来说,这看起来像是单个视图正在优雅地重绘自身。 (如果您有必须通过变换手动旋转的非标准 UI 元素,这尤其有用)

You set every view controller to say that it responds to any possible orientation. Therefore, every view will attempt to rotate to every orientation.

Views don't really automatically rotate. You usually have to manage the placement of subview programmatically in all but the simplest views.

If you have no custom orientation code, you're probably seeing the views try to draw the portrait view in the landscape frame or vice versa. If you have autoresize subviews set your subviews will appear to scatter across the screen in a seemingly random pattern. The more you change orientation, the more random the placement becomes.

For complex views, I like to create separate viewController/view pairs for each orientation. Then I put the views in a nav controller. As the orientation changes, each view controller will push or pop the appropriate view controller for the coming orientation onto/off the stack. To the user, this looks like a single view is gracefully redrawing itself. (This is especially useful if you have non-standard UI elements that have to be manually rotated with transforms)

花落人断肠 2024-09-01 10:28:16

您必须子类化 UITabBarController 并实现 shouldAutorotateToInterfaceOrientation:

You have to subclass UITabBarController and implement shouldAutorotateToInterfaceOrientation:

挽袖吟 2024-09-01 10:28:16

实际上,我只想旋转我的第一个选项卡视图控制器。所以我把这段代码放在我的自定义 tabBarController 中:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
        if (self.selectedIndex == 0) {
            return toInterfaceOrientation != UIInterfaceOrientationPortraitUpsideDown;
        }else {
            return toInterfaceOrientation == UIInterfaceOrientationPortrait;
        }
}

但我遇到了同样的问题。当转向横向时,我为我的第一个选项卡视图控制器使用自定义方向代码。在我的自定义 tabBarcontroller 中使用以下函数调用:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    if (toInterfaceOrientation == UIInterfaceOrientationPortrait) {
        //rotation to Portrait
        lastOrientation = toInterfaceOrientation;
        [[UIApplication sharedApplication] setStatusBarHidden:NO animated:NO];
        [self.selectedViewController willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
    }
    else if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
        if (!UIInterfaceOrientationIsLandscape(lastOrientation)) {
            //rotation to Landscape
            [self.selectedViewController willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
        }
        lastOrientation = toInterfaceOrientation;
    }
}

Actually, I just want my first tab view controller to rotate. So I put this code in my custom tabBarController :

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
        if (self.selectedIndex == 0) {
            return toInterfaceOrientation != UIInterfaceOrientationPortraitUpsideDown;
        }else {
            return toInterfaceOrientation == UIInterfaceOrientationPortrait;
        }
}

but I had the same problem. I use a custom orientation code for my first tab view controller when turning to landscape. Called with the following function in my custom tabBarcontroller:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    if (toInterfaceOrientation == UIInterfaceOrientationPortrait) {
        //rotation to Portrait
        lastOrientation = toInterfaceOrientation;
        [[UIApplication sharedApplication] setStatusBarHidden:NO animated:NO];
        [self.selectedViewController willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
    }
    else if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
        if (!UIInterfaceOrientationIsLandscape(lastOrientation)) {
            //rotation to Landscape
            [self.selectedViewController willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
        }
        lastOrientation = toInterfaceOrientation;
    }
}
纸短情长 2024-09-01 10:28:16

我发现如果您以编程方式设置选定的选项卡,tabViewController 会不稳定地旋转。

I found that if you set the selected tab programmatically the tabViewController rotates erratically.

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