Skip to content

Instantly share code, notes, and snippets.

@dobster
Created October 29, 2018 11:54
Show Gist options
  • Save dobster/c18be48618e491eebf56c859b650774e to your computer and use it in GitHub Desktop.
Save dobster/c18be48618e491eebf56c859b650774e to your computer and use it in GitHub Desktop.
Add Close button to a popover that presents full screen modal in horizontal compact
import UIKit
class ViewController: UIViewController, UIPopoverPresentationControllerDelegate {
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
super.prepare(for: segue, sender: sender)
segue.destination.popoverPresentationController?.delegate = self
}
// MARK: - UIPopoverPresentationControllerDelegate
func presentationController(_ controller: UIPresentationController,
viewControllerForAdaptivePresentationStyle style: UIModalPresentationStyle) -> UIViewController? {
if let navController = controller.presentedViewController as? UINavigationController {
navController.children.first?.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Close", style: .plain, target: self, action: #selector(didTapClose(_:)))
}
return controller.presentedViewController
}
func presentationController(_ presentationController: UIPresentationController,
willPresentWithAdaptiveStyle style: UIModalPresentationStyle,
transitionCoordinator: UIViewControllerTransitionCoordinator?) {
if let navController = presentationController.presentedViewController as? UINavigationController, style == .none {
navController.children.first?.navigationItem.rightBarButtonItem = nil
}
}
@objc func didTapClose(_ sender: Any) {
dismiss(animated: true, completion: nil)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment