【问题标题】:issue with positioning of pop over view弹出视图的定位问题
【发布时间】:2011-04-21 01:33:22
【问题描述】:

当我在 iPad 中的纵向视图和横向视图 (&Vice Versa) 之间切换时,我的弹出视图的位置会出现乱码。这是我计算弹出视图框架的方法:

aRect = self.myElement.frame;
aRect.origin.x += aRect.size.width;

[aPopOver presentPopoverFromRect:aRect inView:self.myElement.superview permittedArrowDirections:UIPopoverArrowDirectionRight animated:YES];

请告诉我这里出了什么问题?

【问题讨论】:

    标签: iphone objective-c ipad uipopovercontroller


    【解决方案1】:

    来自UIPopoverController documentation:(强调我的)

    如果用户旋转设备,而 弹出框可见,弹出框 控制器隐藏弹出框,然后 在结尾再次显示 回转。弹出框控制器 尝试定位弹出框 适合你,但你可能有 再次呈现或隐藏 在某些情况下完全一致。例如, 从条形按钮项目显示时, 弹出框控制器自动 调整位置(并且可能 要考虑的弹出框的大小) 用于更改栏的位置 按钮项目。但是,如果您删除 期间的栏按钮项 轮播,或如果您展示了 从 a 中的目标矩形弹出 查看,弹出框控制器没有 尝试重新定位弹出框。在 那些情况,你必须手动隐藏 弹出窗口或再次从 一个合适的新职位。你可以 在 didRotateFromInterfaceOrientation: 你的视图控制器的方法 用于呈现弹出框。

    【讨论】:

    • 好的。但是我应该在 'didRotateFromInterfaceOrientation' 方法中编写什么代码。我的弹出显示逻辑与控制器方法紧密耦合,而且我的视图由自定义 UI 小部件组成。在一个这样的 UI 小部件上,我正在展示这个弹出视图。
    【解决方案2】:

    好的,我注意到你的代码有些奇怪。

    您有什么理由将宽的大小添加到 aRect 的 x 位置的原点? aRect.origin.x += aRect.size.width;

    我假设你希望这是右上角....

    您可以取消注释 .m 文件中的代码并使其如下所示:

    // Override to allow orientations other than the default portrait orientation.
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
         Return YES; // for supported orientations
        //otherwise return (interfaceOrientation == UIInterfaceOrientationLandscape); if you want only landscape mode.
    }
    

    或者如果你想布局子视图,我会在你的情况下使用 didRotateFromIntferfaceOrientation: 像这样:

    (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        [self layoutSubviews];
    }
    

    还有layoutSubviews

    - (void)layoutSubviews
    {
        NSLog(@"layoutSubviews called");
    
        ...recalc rects etc based on the new self.view.bounds...
    }
    

    它是这样工作的。

    PK

    【讨论】:

    • 是的,我正在尝试在弹出视图中间对齐右侧。通过在原点添加宽度,我可以在启动应用程序时将其放置在正确的位置。当我改变方向时问题就来了。
    • 所以你应该尝试的是 didRotateFromInterfaceOrientation 我会更新我的帖子,告诉你如何做到这一点。
    • 只是一个问题,您的应用程序中有标签栏吗?
    • 好的。但是我应该在 'didRotateFromInterfaceOrientation' 方法中编写什么代码。我的弹出显示逻辑与控制器方法紧密耦合,而且我的视图由自定义 UI 小部件组成。在一个这样的 UI 小部件上,我正在展示这个弹出视图。
    • @abhinav 在您的 didRotateFromInterfaceOrientation 中您只需编写 [self layoutSubviews];这是一个你应该创建的方法,它只会包含只更新你的 GUI 的代码。就这样。并且在您的 layoutSubviews 方法中,该方法将再次包含将更新图形用户界面的代码。所以你会有 button.x = here 和 aRect.origin.x += where;并重新定位显示器,使其适合横向模式。就是这样。用控制器方法填充的显示逻辑无关紧要。您只需要更新 GUI。
    【解决方案3】:

    这是一个老问题,但我发现 OP 不清楚他应该如何处理 Kris Markel 的建议。这记录在Technical Q&A QA1694 中。只需再次显示弹出框。

    假设您已将弹出框代码放在名为showPopover 的方法中。只需在旋转时调用该方法,首先确保它是可见的:

    -(void)showPopover {
        aRect = self.myElement.frame;
        aRect.origin.x += aRect.size.width;
    
        [self.aPopOver presentPopoverFromRect:aRect inView:self.myElement.superview permittedArrowDirections:UIPopoverArrowDirectionRight animated:YES];
    }
    
    -(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)oldOrientation
    {
        if ([self.aPopover isPopoverVisible]) {
            [self showPopover];
        }
    }
    

    【讨论】:

      【解决方案4】:

      在视图控制器中使用下面提到的 UIPopoverPresentationControllerDelegate 方法。

      func popoverPresentationController(_ popoverPresentationController: UIPopoverPresentationController,
                                         willRepositionPopoverTo rect: UnsafeMutablePointer<CGRect>,
                                         in view: AutoreleasingUnsafeMutablePointer<UIView>) {
          rect.pointee = self.view.bounds
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-01-28
        • 1970-01-01
        • 2015-03-14
        • 1970-01-01
        • 1970-01-01
        • 2015-02-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多