Last active
October 4, 2018 16:57
-
-
Save benbahrenburg/c5800be41a6c94e98a720f3d95144d0b 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