Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.