Last active
August 3, 2018 15:51
-
-
Save JasonCanCode/47069b64ec9159aba629bf61fd5c5fc2 to your computer and use it in GitHub Desktop.
Menu Delegate for easily handling a side drawer nav
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 | |
/** | |
ParentViewController should be embedded in a UINavigationController in the Main storyboard. It will consist of two container views side by side. The left will contain a TableViewController for the SideMenuViewController and the right (`containerView`) will be the width of the device and house the `contentViewController`. A toolbar sits atop the containerView as a mock nav bar for the root views. | |
*/ | |
class ParentViewController: UIViewController, MenuDelegate { | |
@IBOutlet weak var titleLabel: UILabel! | |
@IBOutlet weak var containerView: UIView! | |
@IBOutlet weak var menuWidthConstraint: NSLayoutConstraint! | |
weak var contentViewController: UIViewController! | |
override func viewWillAppear(_ animated: Bool) { | |
super.viewWillAppear(animated) | |
menuWidthConstraint.constant = 0 | |
navigationController?.isNavigationBarHidden = true | |
} | |
override func viewWillDisappear(_ animated: Bool) { | |
super.viewWillDisappear(animated) | |
navigationController?.isNavigationBarHidden = false | |
} | |
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { | |
if segue.identifier == "embedInitialContent" { | |
contentViewController = segue.destination | |
} else if let menuVC = segue.destination as? SideMenuViewController { | |
menuVC.delegate = self | |
} | |
} | |
// MARK: - Navigation | |
func showTimeline() { | |
if let nextViewController = newContentViewController(forClass: TimelineViewController.self) { | |
replaceContent(with: nextViewController) | |
} | |
dismissSideMenu() | |
} | |
func showProfile() { | |
if let nextViewController = newContentViewController(forClass: ProfileViewController.self) { | |
replaceContent(with: nextViewController) | |
} | |
dismissSideMenu() | |
} | |
func showMessages() { | |
if let nextViewController = newContentViewController(forClass: MessagesViewController.self) { | |
replaceContent(with: nextViewController) | |
} | |
dismissSideMenu() | |
} | |
func showSettings() { | |
if let nextViewController = newContentViewController(forClass: SettingsViewController.self) { | |
replaceContent(with: nextViewController) | |
} | |
dismissSideMenu() | |
} | |
func logout() { | |
repository.logout() | |
performSegue(withIdentifier: "unwindToLogin", sender: nil) | |
} | |
} | |
// MARK: - Navigation Bar Handling | |
extension ParentViewController: MenuDelegate, ParentNavigationDelegate { | |
@IBAction private func menuButtonPressed(_ sender: UIBarButtonItem) { | |
toggleMenu() | |
} | |
func setNavBar(title: String) { | |
titleLabel.text = title | |
} | |
} |
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 | |
/// Identifies a root View Controller for a menu feature. Since a custom nav bar replacement is used, this is needed to set the title. | |
protocol TopLevelViewControllerType { | |
var navigationTitle: String { get } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This makes use of StoryboardHelper: https://gist.github.com/JasonCanCode/78006e7bf5c670376eda5d7fee0ad6c4