Search code examples
iossegueuipopover

iOS Popover Is Displaying On TextView


I have implemented popovers as UIViewController presentations. I use a modal presentation style of Popover and UIPopoverPresentationController as the main view.

let popoverViewController = segue.destinationViewController as! SearchTableViewController
self.searchTableViewController = popoverViewController
popoverViewController.modalPresentationStyle = UIModalPresentationStyle.Popover
popoverViewController.popoverPresentationController!.delegate = self

My problem is that the popovers are displaying over the TextViews that they are anchored to.

Ex:

enter image description here

Here is how I have the TextView and the popover wired up on the story board:

enter image description here

enter image description here

Does anyone have an idea why the popover is pointing to the top left of this TextView? This was working fine with iOS 8 but the popovers shifted slightly in iOS9.


Solution

  • I am not sure why this was occurring but the quick solution is to move the frame where you want it. I did this with the code below:

    let absoluteframe = patientTextField!.convertRect(patientTextField!.frame, fromView: self.scrollView)
    popoverViewController.popoverPresentationController!.sourceRect = CGRectMake(absoluteframe.minX + 60,absoluteframe.minY + 20,0,0)
    

    First, you need to get the frame for the item the popup is anchored to. Then you can set the sourceRect by updating the rectangle position with their current x and y values. Hope this helps anyone with similar problems!