iOS 5 多故事板旋转

发布于 2025-01-06 01:57:04 字数 929 浏览 2 评论 0原文

我有一个 iPad 应用程序,有 2 个视图,每个视图都有自己的视图控制器。苹果表示,他们建议 iPad 应用程序能够向各个方向旋转。我使用这种技术在旋转变化时定位故事板中的元素:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    //[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];

    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight || toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft)
    {
        studentNumberButton.frame = CGRectMake(30, 1, 158, 155);
        studentNumberLabel.frame = CGRectMake(173, 57, 347, 43);
        studentLabel.frame = CGRectMake(528, 56, 67, 43);

        // Other code goes below...

我对每个视图都执行了此操作,并且它对于所有旋转都适用。然而,当我说,将 iPad 放在横向视图控制器 1 上并按下按钮以显示视图控制器 2 时,问题就出现了,视图控制器 2 效果很好,但处于纵向模式。如果我处于视图控制器 2 的横向模式并转到视图控制器 1,也会发生同样的情况。如果我处于纵向模式,则两个视图的一切都很好。

我怎样才能做到当我切换视图时,视图知道设备所在的方向并相应地旋转?

谢谢!

I have an iPad app that has 2 views each with its own View Controller. Apple states they suggest iPad apps be able to rotate in all directions. I used this technique to position the elements in my storyboard on rotation changes:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    //[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];

    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight || toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft)
    {
        studentNumberButton.frame = CGRectMake(30, 1, 158, 155);
        studentNumberLabel.frame = CGRectMake(173, 57, 347, 43);
        studentLabel.frame = CGRectMake(528, 56, 67, 43);

        // Other code goes below...

I did this for each view and it works fine for all rotations. However the issue comes up when I say, have the the iPad on View Controller 1 in landscape and press a button to show View Controller 2, View Controller 2 comes up just fine but is in Portrait mode. Same happens if I am in landscape mode for View Controller 2 and go to View Controller 1. If I am in portrait mode, everything is fine for both views.

How can i make it so when I switch views, the view knows what orientation the device is in and rotates accordingly?

Thanks!

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

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

发布评论

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

评论(1

春庭雪 2025-01-13 01:57:04

您需要添加一个viewWillAppear方法,如下所示:

- (void)viewWillAppear:(BOOL)animated {
if (([self interfaceOrientation] == UIDeviceOrientationLandscapeLeft)|| ([self interfaceOrientation] == UIDeviceOrientationLandscapeRight)) {

    studentNumberButton.frame = CGRectMake(30, 1, 158, 155);
    studentNumberLabel.frame = CGRectMake(173, 57, 347, 43);
    studentLabel.frame = CGRectMake(528, 56, 67, 43);
    ...

} else {
    ...
}

}

You need to add a viewWillAppear method as follows:

- (void)viewWillAppear:(BOOL)animated {
if (([self interfaceOrientation] == UIDeviceOrientationLandscapeLeft)|| ([self interfaceOrientation] == UIDeviceOrientationLandscapeRight)) {

    studentNumberButton.frame = CGRectMake(30, 1, 158, 155);
    studentNumberLabel.frame = CGRectMake(173, 57, 347, 43);
    studentLabel.frame = CGRectMake(528, 56, 67, 43);
    ...

} else {
    ...
}

}

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