Skip to content

Instantly share code, notes, and snippets.

@rajohns08
Created November 9, 2015 02:51
Show Gist options
  • Save rajohns08/771e756fe47d256bcb57 to your computer and use it in GitHub Desktop.
Save rajohns08/771e756fe47d256bcb57 to your computer and use it in GitHub Desktop.
iOS / Swift - A base view controller for dismissing keyboard and applying color theme
import UIKit
class BaseViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
listenForTapsOnViewToDismissKeyboard()
applyGlobalColorTheme()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func listenForTapsOnViewToDismissKeyboard() {
let tapRecognizer = UITapGestureRecognizer(target: self, action: "dismissKeyboard")
self.view.addGestureRecognizer(tapRecognizer)
}
func dismissKeyboard() {
self.view.endEditing(true)
}
private func applyGlobalColorTheme() {
setBackgroundColor()
styleNavBar()
}
private func setBackgroundColor() {
self.view.backgroundColor = UIColor.grayColor()
}
private func styleNavBar() {
setNavBarColor()
setNavBarTitleTextColor()
setNavBarBackTextColor()
}
private func setNavBarColor() {
self.navigationController?.navigationBar.barTintColor = UIColor.lightGrayColor()
}
private func setNavBarTitleTextColor() {
self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.grayColor()]
}
private func setNavBarBackTextColor() {
self.navigationController?.navigationBar.tintColor = UIColor.blueColor()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment