title: Position a UIView at point of tap to launch popover
body: <p>In what I think is a rather clever approach to getting around the problem of launching a popover from a <code>UITableViewRowAction</code>, I want to position a clear <code>UIView</code> over the target action and use it for the <code>sourceRect</code> to anchor the <code>UIPopoverPresentationController</code>. In this case the target action is the orange "Acct" button shown in the screenshot below.</p>
  
  <p>I'm using a clear <code>UIView</code> (<code>self.clearTopView</code>) over the entire tableview and I init the launching view (<code>tappedViewOverlay</code>) with a frame derived from (so I thought) the location of the tap (<code>point</code>) in the view and a <code>CGSize</code> (<code>tappedRectSize</code>). Then I add it as a subview to <code>self.clearTopView</code>. I'm currently coloring it black so I can find it. </p>
  
  <p>As shown in the following screenshot, part of the scheme is working according to plan. <code>tappedViewOverlay</code> view is added, and colored appropriately. The <code>popoverPresentationController</code> (<code>thisPPC</code>) is launched from this view, as intended. </p>
  
  <p><a href="https://i.stack.imgur.com/jlxST.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jlxST.png" alt="enter image description here"></a></p>
  
  <p>Problem is that the view is always positioned in the upper left corner of the screen, instead of the desired location, which is where the tap occurred. Here's the relevant code:</p>
  
  <pre><code>    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;
  </code></pre>
  
  <p><strong>Can someone please show me where I'm going wrong?</strong></p>
  
  <p><strong>Edit</strong></p>
  
  <p>CoderWang pointed out an oversight in my code, so I've corrected it:</p>
  
  <pre><code>    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;
  </code></pre>
  
  <p>This change (initialization of the <code>tapGestureRecognizer</code> and addition to <code>self.clearTopView</code>) produces a slight change in the position of the black <code>tappedViewOverlay</code> view. You can see it hiding behind the "Back" nav button in the following screenshot:</p>
  
  <p><a href="https://i.stack.imgur.com/bOyyR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bOyyR.png" alt="enter image description here"></a></p>