0

I am in a quandary about how to position the popup on the supplied screenshot to point to the word tapped. The word in the text view is the one that is grey. I would show some code but I have none that is relevant other than the code that handles the tap.

Here is some code:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.

    if(segue.identifier == "popOverSegue") {
        let destinationViewController: PopoverViewController = segue.destination as! PopoverViewController
        destinationViewController.modalPresentationStyle = UIModalPresentationStyle.popover
        destinationViewController.popoverPresentationController!.delegate = self
        destinationViewController.popoverPresentationController?.permittedArrowDirections = .down
        destinationViewController.tappedWord = tappedWord
        destinationViewController.definitionText = "This is a test description"

        let x = meaningText.frame.minX
        let y = meaningText.frame.minY

        destinationViewController.popupOrigin = CGPoint(x: x + pointOfTap.x,y:  y + pointOfTap.y)
        destinationViewController.popupSize = CGSize(width: 200, height: 200)


        debugPrint(meaningText)
    }
}

Screenshot on iPhone 6s

doodle
  • 47
  • 7

1 Answers1

0

Not sure if these are applicable:

If you are using a UIButton() for the highlighted text, you could pretty easily get the button's location using something like the following:

let buttonLocation = theButton.frame.origin
print("Buttons location is \(buttonLocation)")

Alternatively, if you're using a UIGestureRecognizer to open the popover, you could use location(in: UIView) to get the tap location (which would be somewhere on/near the highlighted text.

That would go something like this:

override func viewDidLoad() {
    let tapView = UITapGestureRecognizer(target: self, action: #selector(TheVC.popover(recognizer:)))
    self.view.isUserInteractionEnabled = true
    self.view.addGestureRecognizer(tapView)
}

@objc func popover(recognizer: UIGestureRecognizer) {
    let tapLocation = recognizer.location(in: self.view)
    print("Tap occurred at location \(tapLocation)")
}

Once you have the location of the tap or the UI element that triggers the tap, you could calculate the popover position from there.

Archdoog
  • 822
  • 9
  • 8
  • Calculating the position of the word relative to the text view is what I am having a hard time calculating. I have the point where the user taps the word, and I have the word itself. I just 1) Cannot seem to reposition the popup without losing it from the screen entirely and 2) cannot get the position of the text view relative to its view controller. – doodle Apr 05 '18 at 15:08
  • For #1, Without seeing code to see what you're referencing, I'd make sure you're calculating and setting your `(x, y)` for the popover frame based on the UIViewController's `self.view`, that way is bounded by something consistent. As for #2, the location of specific text inside a UITextView, maybe something like this https://stackoverflow.com/questions/28468187/find-cgpoint-location-of-substring-in-textview? Hard to judge what's causing those issues without more details. – Archdoog Apr 05 '18 at 15:22
  • I'm curious what the x, y values of `meaningText.frame` and `pointOfTap` are if you were to print them around where that debugPrint is. – Archdoog Apr 05 '18 at 21:00
  • meaningText.frame.minX = 67.0 meaningText.frame.minY = 344.0 tappedWord = (172.5, 35.0) – doodle Apr 06 '18 at 08:24