Skip to content

Instantly share code, notes, and snippets.

@marcocapano
Last active November 17, 2018 16:20
Show Gist options
  • Save marcocapano/1a58dad3ff75a017a506436954682b18 to your computer and use it in GitHub Desktop.
Save marcocapano/1a58dad3ff75a017a506436954682b18 to your computer and use it in GitHub Desktop.
Helper function to present a popover.
///
/// - Parameters:
/// - popoverContent: the view controller to add as a popover.
/// - sourcePoint: the point in which to anchor the popover.
/// - size: the size of the popover. Default uses the popover preferredContentSize.
/// - delegate: the popover's presentationController delegate. Default is nil.
/// - animated: Pass true to animate the presentation; otherwise, pass false.
/// - completion: The block to execute after the presentation finishes. Default is nil.
public func presentPopover(_ popoverContent: UIViewController, sourcePoint: CGPoint, size: CGSize? = nil, delegate: UIPopoverPresentationControllerDelegate? = nil, animated: Bool = true, completion: (() -> Void)? = nil) {
popoverContent.modalPresentationStyle = .popover
if let size = size {
popoverContent.preferredContentSize = size
}
if let popoverPresentationVC = popoverContent.popoverPresentationController {
popoverPresentationVC.sourceView = view
popoverPresentationVC.sourceRect = CGRect(origin: sourcePoint, size: .zero)
popoverPresentationVC.delegate = delegate
}
present(popoverContent, animated: animated, completion: completion)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment