Skip to content

Instantly share code, notes, and snippets.

@kevinylu
Forked from benbahrenburg/BaseController.swift
Created August 23, 2018 22:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinylu/d53d322d515ba808c199a9b912a2c721 to your computer and use it in GitHub Desktop.
Save kevinylu/d53d322d515ba808c199a9b912a2c721 to your computer and use it in GitHub Desktop.
Adding Consistent Back Button to view
import UIKit
class BaseViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let backTitle = NSLocalizedString("Back", comment: "Back button label")
self.addBackbutton(backTitle)
}
}
import UIKit
extension UIViewController {
func backButtonAction() {
self.dismissViewControllerAnimated(true, completion: nil)
}
func addBackbutton(title: String) {
if let nav = self.navigationController,
let item = nav.navigationBar.topItem {
item.backBarButtonItem = UIBarButtonItem(title: title, style: UIBarButtonItemStyle.Plain, target: self, action:
#selector(self.backButtonAction))
} else {
if let nav = self.navigationController,
let _ = nav.navigationBar.backItem {
self.navigationController!.navigationBar.backItem!.title = title
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment