Skip to content

Instantly share code, notes, and snippets.

@Gonzalo-MR8
Last active April 27, 2023 11:55
Show Gist options
  • Save Gonzalo-MR8/3b1b2e5627fd4f2b428be67b0e4ff92c to your computer and use it in GitHub Desktop.
Save Gonzalo-MR8/3b1b2e5627fd4f2b428be67b0e4ff92c to your computer and use it in GitHub Desktop.
This is a custom UINavigationController to manage navigation between each ViewController, it will be used only to navigate with UIKit and work with .xib and not with .storyboard. To know how to use it you can go to see how I use it in the InfoSpace project, which is in my github account. If you have any questions about how to use it, ask away!
//
// CustomNavigationController.swift
//
// Created by GonzaloMR
//
import UIKit
import SafariServices
final class CustomNavigationController: UINavigationController {
// Here we have to create and instantiate the rootViewController
static let instance = CustomNavigationController(rootViewController: SplashScreenViewController.initAndLoad())
private override init(rootViewController: UIViewController) {
super.init(rootViewController: rootViewController)
// Configure Navigation Controller appearance
self.view.translatesAutoresizingMaskIntoConstraints = false
self.setNavigationBarHidden(true, animated: false)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func configureWindow() -> UIWindow {
// Create a window that is the same size as the screen
let window = UIWindow(frame: UIScreen.main.bounds)
window.rootViewController = self
// Show the window
window.makeKeyAndVisible()
return window
}
func navigate(to destination: UIViewController, animated: Bool, closePreviousVC: Bool = false) {
if let topViewController = UIApplication.topViewController(controller: self), let nav = topViewController.navigationController {
nav.pushViewController(destination, animated: animated)
if closePreviousVC {
if let index = nav.viewControllers.firstIndex(of: destination), index > 0 {
nav.viewControllers.removeSubrange(0..<index)
}
}
} else {
self.pushViewController(destination, animated: animated)
if closePreviousVC {
if let index = self.viewControllers.firstIndex(of: destination), index > 0 {
self.viewControllers.removeSubrange(0..<index)
}
}
}
}
func present(to destination: UIViewController, animated: Bool, presentationStyle: UIModalPresentationStyle = .overCurrentContext, completion: (() -> Void)? = nil) {
if let topViewController = UIApplication.topViewController(controller: self) {
destination.modalPresentationStyle = presentationStyle
topViewController.present(destination, animated: animated, completion: completion)
} else {
destination.modalPresentationStyle = presentationStyle
self.present(destination, animated: animated, completion: completion)
}
}
func dismissVC(animated: Bool, completion: (() -> Void)? = nil) {
if let topViewController = UIApplication.topViewController(controller: self) {
if topViewController.isModal() {
topViewController.dismiss(animated: animated, completion: completion)
} else {
topViewController.navigationController?.popViewController(animated: animated)
completion?()
}
} else {
self.popViewController(animated: animated)
completion?()
}
}
func presentDefaultAlert(title: String, message: String, actionTitle: String = "Okey", completion: ((UIAlertAction) -> Void)? = nil) {
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: actionTitle, style: .default, handler: completion))
self.present(alert, animated: true, completion: nil)
}
func presentAcceptOrCancelAlert(title: String, message: String, acceptActionTitle: String = "Accept", cancelActionTitle: String = "Cancel", acceptCompletion: ((UIAlertAction) -> Void)? = nil, cancelCompletion: ((UIAlertAction) -> Void)? = nil) {
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
let acceptAction = UIAlertAction(title: acceptActionTitle, style: .default, handler: acceptCompletion)
let cancelAction = UIAlertAction(title: cancelActionTitle, style: .default, handler: cancelCompletion)
alert.addAction(acceptAction)
alert.addAction(cancelAction)
self.present(alert, animated: true, completion: nil)
}
func popToViewController(classVC: AnyClass, animated: Bool) {
if let viewController = self.getViewControllerInStack(classVC: classVC) {
self.popToViewController(viewController, animated: animated)
}
}
func getViewControllerInStack(classVC: AnyClass) -> UIViewController? {
return self.viewControllers.first(where: { type(of:$0) == classVC })
}
func openUrl(_ url: URL) {
let safariController = SFSafariViewController(url: url)
safariController.modalPresentationStyle = .overFullScreen
self.present(safariController, animated: true, completion: nil)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment