如果用户在查看 MasterViewController 时旋转,则 DetailViewController 的主从应用程序自动旋转不会对齐

发布于 2024-12-15 23:06:26 字数 1075 浏览 1 评论 0原文

我有一个简单的主从应用程序,带有 MasterViewController 和 DetailViewController。在 DetailViewController 内部,我实现了以下代码,以便在旋转视图时我的两个按钮能够正确定位。

  - (void)willAnimateRotationToInterfaceOrientation:
(UIInterfaceOrientation)toInterfaceOrientation 
                                         duration:(NSTimeInterval)duration
{
    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft)
    {   firstButton.frame = CGRectMake(20, 240, 50, 50);
        secondButton.frame = CGRectMake(390, 240, 50, 50);
    }
    else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
    {
        firstButton.frame = CGRectMake(20, 240, 50, 50);
        secondButton.frame = CGRectMake(390, 240, 50, 50);
    }
    else
    {
        firstButton.frame = CGRectMake(80, 390, 50, 50);
        secondButton.frame = CGRectMake(190, 390, 50, 50);
    }
}

如果 iPhone 直到 DetailViewController 出现在视图中才旋转,则按钮的位置正确。但是,如果用户在查看 MasterViewController 表格视图时旋转 iPhone,然后点击某个项目,则当 DetailViewController 打开时,按钮的位置不正确。

那么我如何或在哪里放置 MasterViewController 中的按钮呢?谢谢您的帮助!

I have a simple Master-Detail application with a MasterViewController and a DetailViewController. Inside of the DetailViewController I have implemented the following code so that when the view is rotated my two buttons are positioned properly.

  - (void)willAnimateRotationToInterfaceOrientation:
(UIInterfaceOrientation)toInterfaceOrientation 
                                         duration:(NSTimeInterval)duration
{
    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft)
    {   firstButton.frame = CGRectMake(20, 240, 50, 50);
        secondButton.frame = CGRectMake(390, 240, 50, 50);
    }
    else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
    {
        firstButton.frame = CGRectMake(20, 240, 50, 50);
        secondButton.frame = CGRectMake(390, 240, 50, 50);
    }
    else
    {
        firstButton.frame = CGRectMake(80, 390, 50, 50);
        secondButton.frame = CGRectMake(190, 390, 50, 50);
    }
}

If the iPhone isn't rotated until the DetailViewController is in view, then the buttons are positioned properly. But if the user rotates the iPhone while viewing the MasterViewController table view and then taps an item, when the DetailViewController opens, the buttons are not positioned correctly.

So how can I or where do I position the buttons from the MasterViewController? Thank you for the help!

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

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

发布评论

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

评论(1

月亮是我掰弯的 2024-12-22 23:06:26

您可以在 viewDidLoad 或 viewWillAppear 中检查 UIViewControllerinterfaceOrientation 属性,例如:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    UIInterfaceOrientation orientation = self.interfaceOrientation;
    if (orientation == UIInterfaceOrientationLandscapeLeft)
    {   firstButton.frame = CGRectMake(20, 240, 50, 50);
        secondButton.frame = CGRectMake(390, 240, 50, 50);
    }
    else if (orientation == UIInterfaceOrientationLandscapeRight)
    {
        firstButton.frame = CGRectMake(20, 240, 50, 50);
        secondButton.frame = CGRectMake(390, 240, 50, 50);
    }
    else
    {
        firstButton.frame = CGRectMake(80, 390, 50, 50);
        secondButton.frame = CGRectMake(190, 390, 50, 50);
    }
}

You can check interfaceOrientation property of UIViewController in your viewDidLoad or viewWillAppear for example:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    UIInterfaceOrientation orientation = self.interfaceOrientation;
    if (orientation == UIInterfaceOrientationLandscapeLeft)
    {   firstButton.frame = CGRectMake(20, 240, 50, 50);
        secondButton.frame = CGRectMake(390, 240, 50, 50);
    }
    else if (orientation == UIInterfaceOrientationLandscapeRight)
    {
        firstButton.frame = CGRectMake(20, 240, 50, 50);
        secondButton.frame = CGRectMake(390, 240, 50, 50);
    }
    else
    {
        firstButton.frame = CGRectMake(80, 390, 50, 50);
        secondButton.frame = CGRectMake(190, 390, 50, 50);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文