将UIPopoverController置于相同位置,仅更改箭头偏移量

时间:2012-07-05 05:34:50

标签: objective-c ios ios5 uipopovercontroller

我的目标是为UIPopoverController保持相同的坐标,只需改变箭头偏移量。 所以基本上我有三个按钮触摸每个按钮显示一个弹出窗口。当呈现此弹出窗口时,它会更改屏幕上的位置,但我不希望这样。 要更清楚地看一下screenoshots:

enter image description here

enter image description here

enter image description here

3 个答案:

答案 0 :(得分:15)

对于我的popover,我希望箭头位于左上角而不是顶部中心(默认情况下)。

我设法通过设置UIPopoverController的popoverLayoutMargins属性来获得下面的结果(屏幕截图)。您可以使用它来减少UIPopoverController内部计算中使用的屏幕区域,以确定弹出窗口的显示位置。

UIPopovercontroller arrow on the left

代码:

// Get the location and size of the control (button that says "Drinks")
CGRect rect = control.frame;

// Set the width to 1, this will put the anchorpoint on the left side
// of the control
rect.size.width = 1;

// Reduce the available screen for the popover by creating a left margin
// The popover controller will assume that left side of the screen starts
// at rect.origin.x
popoverC.popoverLayoutMargins = UIEdgeInsetsMake(0, rect.origin.x, 0, 0);

// Simply present the popover (force arrow direction up)
[popoverC presentPopoverFromRect:rect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];

我认为你可以通过调整上述内容来获得理想的结果。

答案 1 :(得分:1)

Apple无法使用Apple的内置UIPopoverViewController类。但是实现你自己的popover视图控制器应该是相当简单和合乎逻辑的(只是一些非常基本的2D几何和UIView的文档中的一些挖掘)。

答案 2 :(得分:1)

是的,你可以这样做。您必须使用alpha = 0.0f创建辅助视图,并使用它来引导箭头。

例如:

auxView = [[UIView alloc] initWithFrame:firstButton.frame];
auxView.alpha = 0.0 ;
auxView.userInteractionEnabled = NO;
[firstButton.superView addSubview:auxView];
[auxView release];

好的,现在您使用该视图作为箭头指南打开popover。

[thePopoverController presentPopoverFromRect:auxView.bounds inView:auxView
            permitedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];

现在你只需要移动视图:

auxView.frame = secondButton.frame;

如果需要,可以为该移动使用动画。

还有一件事,对于这种箭头按钮,我更喜欢箭头触摸按钮。您可以使用:

presentPopoverFromRect:CGRectInset(auxView.bounds, 4.0, 4.0)