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

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

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

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

有帮助吗?

解决方案

OK,我注意到一个很奇怪你的代码。

要添加的广泛的大小aRect的x位置的起源的原因吗? aRect.origin.x + = aRect.size.width;

IM假设你想这是右上角....

您可以取消的代码在你的.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

其他提示

来自 UIPopoverController 文档: :(强调我的)

如果用户在可见弹出窗口时旋转设备,则弹出控制器将隐藏弹出窗口,然后在旋转结束时再次显示。Popover控制器试图适当地定位弹出窗口,但在某些情况下,您可能必须再次呈现或完全隐藏它。例如,当从条按钮项目显示时,弹出控制器会自动调整弹出窗口的位置(并可能是大小),以说明对条按钮项目的位置的更改。但是,如果在旋转过程中删除条按钮项,或者 如果您在视图中从目标矩形呈现弹出窗口, ,弹出控制器不会尝试重新定位弹出窗口。在这种情况下,您必须手动隐藏弹出窗口或从适当的新位置中再次呈现。您可以在 didRotateFromInterfaceOrientation:您用来呈现弹出器的视图控制器的方法。

这是一个老问题,但我看到现在还不清楚到OP什么,他应该与克里斯·马克尔的建议去做。这在技术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];
    }
}

使用下面提及的方法UIPopoverPresentationControllerDelegate在ViewController中。

func popoverPresentationController(_ popoverPresentationController: UIPopoverPresentationController,
                                   willRepositionPopoverTo rect: UnsafeMutablePointer<CGRect>,
                                   in view: AutoreleasingUnsafeMutablePointer<UIView>) {
    rect.pointee = self.view.bounds
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top