当我在iPad中纵向切换到横向视图(& Vice Versa)时,我的弹出视图的位置会出现乱码。这是我如何计算我的弹出视图的框架:
aRect = self.myElement.frame;
aRect.origin.x += aRect.size.width;
[aPopOver presentPopoverFromRect:aRect inView:self.myElement.superview permittedArrowDirections:UIPopoverArrowDirectionRight animated:YES];
请告诉我这里有什么问题?
答案 0 :(得分:5)
来自UIPopoverController documentation :(强调我的)
如果用户在旋转设备的同时旋转设备 popover是可见的,popover 控制器隐藏弹出窗口然后 在结束时再次显示它 回转。弹出控制器 试图定位弹出窗口 适合你,但你可能有 再次呈现或隐藏它 在某些情况下完全一样。例如, 从条形按钮项显示时, 弹出控制器自动 调整位置(并可能 帐户的popover的大小) 更改栏的位置 按钮项目。但是,如果你删除 期间的栏按钮项目 旋转,或如果你提出 从一个目标矩形弹出 视图,弹出控制器没有 试图重新定位弹出窗口。在 那些情况下,你必须手动隐藏 弹出或再次出现 一个合适的新职位。您可以 这样做 didRotateFromInterfaceOrientation: 你的视图控制器的方法 用来呈现弹出窗口。
答案 1 :(得分:1)
您是否将宽度的大小添加到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 强>
答案 2 :(得分:1)
这是一个老问题,但我看到OP不清楚他应该对Kris Markel的建议做些什么。这在Technical Q&A QA1694中有记录。再次出现一下popover。
假设您已将上面的popover代码放在名为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];
}
}
答案 3 :(得分:0)
在视图控制器中使用下面提到的UIPopoverPresentationControllerDelegate方法。
func popoverPresentationController(_ popoverPresentationController: UIPopoverPresentationController,
willRepositionPopoverTo rect: UnsafeMutablePointer<CGRect>,
in view: AutoreleasingUnsafeMutablePointer<UIView>) {
rect.pointee = self.view.bounds
}