[英]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.