-
-
Save kevinylu/d53d322d515ba808c199a9b912a2c721 to your computer and use it in GitHub Desktop.
Adding Consistent Back Button to view
This file contains 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 | |
class BaseViewController: UIViewController { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
let backTitle = NSLocalizedString("Back", comment: "Back button label") | |
self.addBackbutton(backTitle) | |
} | |
} |
This file contains 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 | |
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