2010-10-04 68 views
4

当我在iPad中纵向切换到横向视图(& Vice Versa)时,弹出窗口视图的位置出现乱码。下面是我如何计算我的popover视图的框架:弹出视图的位置问题

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

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

请告诉我这里有什么问题吗?

回答

1

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

您是否将宽度的大小添加到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

+0

是的,我正在尝试这种方式在弹出视图中间的右侧对齐。通过在原点中添加宽度,我可以在启动应用程序时将它放置在正确的位置。问题出现在我改变方向时。 – Abhinav 2010-10-04 23:24:09

+0

所以你应该尝试的是didRotateFromInterfaceOrientation我会更新我的文章,你可以这样做。 – Pavan 2010-10-04 23:25:12

+0

只是一个问题,你有一个标签栏在你的应用程序? – Pavan 2010-10-04 23:35:17

5

UIPopoverController documentation:(重点煤矿)

如果用户同时 酥料饼是可见的旋转设备中,酥料饼 控制器隐藏酥料饼,然后 示出了它再次在 轮换结束。弹出窗口控制器 尝试适当地为您放置弹出窗口 ,但您可能有 在某些情况下再次显示它或将其隐藏 。例如,从一个栏按钮项显示时 , 的酥料饼的控制器自动 调整位置(和潜在 大小)的酥料饼的占 更改到杆 按钮项的位置。但是,如果你 旋转过程中去除 栏按钮项目,或者如果从目标矩形的 视图提出的 酥料饼中,酥料饼的控制器不 试图重新定位酥料饼。在 这些情况下,您必须手动隐藏 弹出窗口或将其从 再次呈现为适当的新位置。您可以使用 在 didRotateFromInterfaceOrientation: 中执行此操作,您可以使用 用于呈现弹出窗口的视图控制器的方法。

+0

好的。但是,我应该在'didRotateFromInterfaceOrientation'方法中写入哪些代码。我弹出的显示逻辑与控制器方法紧密结合,而且,我的视图由自定义UI小部件组成。在一个这样的UI小部件中,我展示了这个弹出视图。 – Abhinav 2010-10-04 23:45:18

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]; 
    } 
}