Instantly share code, notes, and snippets.

Embed
What would you like to do?
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