Skip to content

Instantly share code, notes, and snippets.

@jeffaburt
Last active June 23, 2022 08:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jeffaburt/8d0d631fc2afebf6fecaca2f91af32f9 to your computer and use it in GitHub Desktop.
Save jeffaburt/8d0d631fc2afebf6fecaca2f91af32f9 to your computer and use it in GitHub Desktop.
Popup Navigation Controller
import UIKit
class PopupNavigationController: UINavigationController {
override init(rootViewController: UIViewController) {
super.init(rootViewController: rootViewController)
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
override init(navigationBarClass: AnyClass?, toolbarClass: AnyClass?) {
super.init(navigationBarClass: navigationBarClass, toolbarClass: toolbarClass)
commonInit()
}
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
commonInit()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
popoverPresentationController?.sourceRect = popoverPresentationController?.sourceView?.bounds ?? .zero
}
private func commonInit() {
modalPresentationStyle = .popover
popoverPresentationController?.delegate = self
popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)
}
}
extension PopupNavigationController: UIPopoverPresentationControllerDelegate {
func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
return .none
}
func popoverPresentationController(_ popoverPresentationController: UIPopoverPresentationController, willRepositionPopoverTo rect: UnsafeMutablePointer<CGRect>, in view: AutoreleasingUnsafeMutablePointer<UIView>) {
rect.initialize(to: view.pointee.bounds)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment