Last active
August 29, 2015 14:18
-
-
Save armstrongnate/c34de1aac8323ac4809a to your computer and use it in GitHub Desktop.
UIViewController extension for setting tabBarItem
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
// 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