Unpredictable delay before UIPopoverController appears under iOS 8.1

南楼画角 提交于 2019-12-03 01:57:37

I found that deselecting the row before attempting to show the popover seems to fix the problem. This may be a useful work-around, but I'm still looking for a better answer, since this may not be robust.

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO]; // adding this line appears to fix the problem
    [self showPopover:[tableView cellForRowAtIndexPath:indexPath]];
}

Note, as Yasir Ali commented, that for this workaround to work, deselect animation must be off. Almost 4 years after the original post, this behavior still affects iOS 12, and the bug report with Apple is still open - sigh.

I had the same issue, and deselecting the cell prior to displaying the popover did nothing to resolve my issue. What did work for me was dispatching the popover code to the main queue with a minuscule delay. This produces consistent display of the popover AND allows me to keep the cell SELECTED, which is key to my UI:

Expected UI

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    switch actions[indexPath.row] {
    case .rename:
          .... 
    case .duplicate:

        DispatchQueue.main.asyncAfter(deadline: .now() + 0.01, execute: {
            let copyController = UIStoryboard.init(name: "Actions", bundle: nil).instantiateViewController(withIdentifier: "copyToNavController")

            copyController.modalPresentationStyle = .popover

            let cell = tableView.cellForRow(at: indexPath)
            copyController.popoverPresentationController?.sourceView = cell
            if let frame = cell?.frame {
                copyController.popoverPresentationController?.sourceRect = frame
            }
            copyController.popoverPresentationController?.permittedArrowDirections = .left

            self.popoverPresenter = copyController.popoverPresentationController
            self.present(copyController, animated: true, completion: nil)
        })
    default:
        break
    }
}

Many thanks for that trick. I have a TableView that displays a popover when I click on a line. My popover was displaying only after a delay, or even was requiring a double click on the line to finally show up. By adding the indicated line ([tableView deselectRowAtIndexPath: indexPath animated: NO]) the popover is displayed immediately without having to double click anymore.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!