Skip to content

Instantly share code, notes, and snippets.

@armstrongnate
Last active August 29, 2015 14:18
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 armstrongnate/c34de1aac8323ac4809a to your computer and use it in GitHub Desktop.
Save armstrongnate/c34de1aac8323ac4809a to your computer and use it in GitHub Desktop.
UIViewController extension for setting tabBarItem
// usage
class MyViewController: UIViewController {
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setupTabBarItem("entries")
}
}
// where "entries" is the name of the image.
// should have 2 images named tabbar-entries and tabbar-entries-selected
extension UIViewController {
func setupTabBarItem(imageName: String) {
var selectedImage = UIImage(named: "tabbar-\(imageName)-selected")
selectedImage = selectedImage?.imageWithRenderingMode(.AlwaysOriginal)
var image = UIImage(named: "tabbar-\(imageName)")
image = image?.imageWithRenderingMode(.AlwaysOriginal)
tabBarItem = UITabBarItem(title: nil, image: nil, tag: 0)
tabBarItem.image = image
tabBarItem.selectedImage = selectedImage
tabBarItem.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0) // insets are optional
tabBarController?.tabBar.tintColor = UIColor.grayColor()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment