Created
October 29, 2018 11:54
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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