[英]Position a UIView at point of tap to launch popover
我认为这是解决从UITableViewRowAction
启动弹出框的问题的一种相当巧妙的方法,我想在目标操作上放置一个清晰的UIView
并将其用作sourceRect
来锚定UIPopoverPresentationController
。 在这种情况下,目标操作是下面的屏幕快照中显示的橙色“ Acct”按钮。
我使用一个明确UIView
( self.clearTopView
)在整个的tableview和我初始化启动视图( tappedViewOverlay
)与衍生自帧(所以我想)的抽头(的位置point
在图和) CGSize
( tappedRectSize
)。 然后,将其作为子视图添加到self.clearTopView
。 我目前正在将其着色为黑色,以便可以找到它。
如以下屏幕截图所示,该方案的一部分正在按计划工作。 添加了tappedViewOverlay
视图,并对其进行了适当的着色。 从该视图按预期启动popoverPresentationController
( thisPPC
)。
问题在于视图始终位于屏幕的左上角,而不是期望的位置(发生轻敲的位置)。 以下是相关代码:
else if ([[segue identifier] isEqualToString:@"AccountPopSegue"])
{
UITapGestureRecognizer *tapGestureRecognizer;
CGPoint point = [tapGestureRecognizer locationInView:self.clearTopView];
NSString *pointString = NSStringFromCGPoint(point);
NSLog(@"point is located at %@",pointString);
CGSize tappedRectSize= CGSizeMake(60,60);
CGRect tappedRect = {point,tappedRectSize};
NSString * tappedRectString = NSStringFromCGRect(tappedRect);
NSLog(@"tappedRect = %@",tappedRectString);
tappedViewOverlay = [[UIView alloc]initWithFrame:tappedRect];
tappedViewOverlay.backgroundColor = [UIColor blackColor];
[self.clearTopView addSubview:tappedViewOverlay];
[self.clearTopView setUserInteractionEnabled:NO];
NSString *tappedOverlayFrame = NSStringFromCGRect(tappedViewOverlay.frame);
NSLog(@"tappedViewOverlay.frame = %@",tappedOverlayFrame);
UIViewController *controller = segue.destinationViewController;
controller.popoverPresentationController.delegate = self;
controller.preferredContentSize = CGSizeMake(320, 186);
UIPopoverPresentationController *thisPPC = controller.popoverPresentationController;
thisNavController = (UINavigationController *)segue.destinationViewController;
AccountChangeVC *acCVC = (AccountChangeVC *)thisNavController.topViewController;
acCVC.delegate = self;
thisPPC.sourceView = self.clearTopView;
thisPPC.sourceRect = tappedViewOverlay.bounds;
有人可以告诉我我要去哪里了吗?
编辑
CoderWang指出了我的代码中的一个疏漏,因此我已更正了它:
else if ([[segue identifier] isEqualToString:@"AccountPopSegue"])
{
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc]init];
[self.clearTopView addGestureRecognizer:tapGestureRecognizer];
CGPoint point = [tapGestureRecognizer locationInView:self.clearTopView];
NSString *pointString = NSStringFromCGPoint(point);
NSLog(@"point is located at %@",pointString);
CGSize tappedRectSize= CGSizeMake(60,60);
CGRect tappedRect = {point,tappedRectSize};
NSString * tappedRectString = NSStringFromCGRect(tappedRect);
NSLog(@"tappedRect = %@",tappedRectString);
tappedViewOverlay = [[UIView alloc]initWithFrame:tappedRect];
tappedViewOverlay.backgroundColor = [UIColor blackColor];
[self.clearTopView addSubview:tappedViewOverlay];
[self.clearTopView setUserInteractionEnabled:NO];
NSString *tappedOverlayFrame = NSStringFromCGRect(tappedViewOverlay.frame);
NSLog(@"tappedViewOverlay.frame = %@",tappedOverlayFrame);
UIViewController *controller = segue.destinationViewController;
controller.popoverPresentationController.delegate = self;
controller.preferredContentSize = CGSizeMake(320, 186);
UIPopoverPresentationController *thisPPC = controller.popoverPresentationController;
thisNavController = (UINavigationController *)segue.destinationViewController;
AccountChangeVC *acCVC = (AccountChangeVC *)thisNavController.topViewController;
acCVC.delegate = self;
thisPPC.sourceView = self.clearTopView;
thisPPC.sourceRect = tappedViewOverlay.bounds;
这一变化(的初始化tapGestureRecognizer
和除了self.clearTopView
)产生在黑色的位置的微小变化tappedViewOverlay
图。 在以下屏幕截图中,您可以看到它隐藏在“返回”导航按钮后面:
问题是'tappedRect'不正确,不知道确切原因。 但是您可以尝试以下方法来将“ Acct”按钮转换为self.clearTopView的矩形:
CGRect tappedRect = [Acct.superview convertRect:acct.frame toView:self.clearTopView];
或转换为窗口(因为self.clearTopView在整个tableview上):
CGRect tappedRect = [Acct.superview convertRect:acct.frame toView:nil];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.