简体   繁体   English

如何使UIPopoverController自动更改其箭头方向

[英]How to make UIPopoverController change its arrow direction automatically

I've encountered strange problem in UIPopOverController. 我在UIPopOverController中遇到了奇怪的问题。 Normally, I present popover this way: 通常,我以这种方式显示弹出窗口:

[popoverVC presentPopoverFromRect:sender.frame
              inView:sender.superview
              permittedArrowDirections:UIPopoverArrowDirectionLeft
              animated:NO];

However, sometimes there is no enough space on the right side of the button (sender) and the popover is displayed below the button with UIPopoverArrowDirectionUP. 但是,有时按钮(发送方)右侧没有足够的空间,并且弹出框通过UIPopoverArrowDirectionUP显示在按钮下方。 It make sense - if popover can't be displayed properly, controller trys to display it with different arrow. 这是有道理的-如果无法正确显示弹出窗口,则控制器会尝试使用其他箭头来显示它。

But when I moved my button (sender) about 10 pixels to the left, popover doesn't behave this way. 但是,当我将按钮(发送器)向左移动约10像素时,弹出窗口不会出现这种情况。 There is still no enough space to display it properly but it doesn't change its arrow although popover is ~20px wide so it's way too small. 仍然没有足够的空间来正确显示它,但是弹出框的宽度约为20像素,因此它不会更改其箭头,因此它太小了。

Is there any way to say popovercontroller: "If popover does not have enough space to display all content, change arrow direction" ? 有没有办法说popovercontroller:“如果popover没有足够的空间来显示所有内容,请更改箭头方向”?

Why not use UIPopoverArrowDirectionAny , which Apple recommends anyway, or, if you absolutely must, UIPopoverArrowDirectionLeft | UIPopoverArrowDirectionUp 为什么不使用Apple建议使用的UIPopoverArrowDirectionAny ,或者,如果绝对必须使用UIPopoverArrowDirectionLeft | UIPopoverArrowDirectionUp UIPopoverArrowDirectionLeft | UIPopoverArrowDirectionUp ? UIPopoverArrowDirectionLeft | UIPopoverArrowDirectionUp The exact orientation of the popover's arrow seldom matters that much. 弹出式箭头的确切方向很少那么重要。

If that doesn't work, I guess the only choice is to do some math in the action method. 如果那不起作用,我猜唯一的选择是在动作方法中做一些数学运算。 (Disclaimer: I've never tried this, but I don't see why you couldn't do it if you absolutely had to.) First convert the sender's bounds to the top-level view's coordinate space. (免责声明:我从未尝试过这样做,但是如果您绝对必须这样做,我不明白为什么您不能这样做。)首先将发件人的边界转换为顶级视图的坐标空间。 Depending on the specifics of your app's view hierarchy, you may need to convert the bounds/frame to/from an appropriate view. 根据应用程序视图层次结构的具体情况,您可能需要将边界/框架与适当的视图之间进行转换。 Unless you've got a something weird going on, this should work: 除非您有奇怪的事情发生,否则这应该可行:

[self.view convertRect:[sender bounds] fromView:sender]

Then check whether the resulting rectangle is more than X points away from the right edge of the screen, where X is the width of the popover (including the black border and arrow). 然后检查所得的矩形是否离屏幕右边缘超过X个点,其中X是弹出框的宽度(包括黑色边框和箭头)。 If there is enough room, present the popover as you say above. 如果有足够的空间,请按照上面的说明显示弹出窗口。 If not, use UIPopoverArrowDirectionUp instead. 如果不是,请改用UIPopoverArrowDirectionUp。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM