Skip to content

Instantly share code, notes, and snippets.

@iAmrSalman
Created August 1, 2017 10:30
Show Gist options
  • Save iAmrSalman/0615962a1c7398603687fb52d8629d11 to your computer and use it in GitHub Desktop.
Save iAmrSalman/0615962a1c7398603687fb52d8629d11 to your computer and use it in GitHub Desktop.
[UITabBarControllerExtension] #swift3 #tabbar
import UIKit
extension UIImage {
class func imageWithColor(color: UIColor, size: CGSize) -> UIImage {
let rect: CGRect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
UIGraphicsBeginImageContextWithOptions(size, false, 0)
color.setFill()
UIRectFill(rect)
let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return image
}
func with(color: UIColor) -> UIImage {
guard let cgImage = self.cgImage else {
return self
}
UIGraphicsBeginImageContextWithOptions(size, false, scale)
let context = UIGraphicsGetCurrentContext()!
context.translateBy(x: 0, y: size.height)
context.scaleBy(x: 1.0, y: -1.0)
context.setBlendMode(.normal)
let imageRect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
context.clip(to: imageRect, mask: cgImage)
color.setFill()
context.fill(imageRect)
let newImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext();
return newImage
}
}
import UIKit
extension UITabBarController {
func removeTabbarItemsText() {
tabBar.items?.forEach {
$0.title = ""
$0.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0)
}
}
func changeUnselectedItemColor(to unselectedColor: UIColor) {
tabBar.items?.forEach {
$0.image = $0.selectedImage!.with(color: unselectedColor).withRenderingMode(.alwaysOriginal)
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName : unselectedColor], for: .normal)
}
}
func changeSelectedItemColor(to selectedColor: UIColor) {
tabBar.tintColor = selectedColor
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName : selectedColor], for: .selected)
}
func changeSelectedItemBackgroundColor(to backgroundColor: UIColor) {
let numberOfItems = CGFloat(tabBar.items!.count)
let tabBarItemSize = CGSize(width: tabBar.frame.width / numberOfItems,
height: tabBar.frame.height)
tabBar.selectionIndicatorImage = UIImage.imageWithColor(color: backgroundColor, size: tabBarItemSize).resizableImage(withCapInsets: .zero)
tabBar.frame.size.width = self.view.frame.width + 4
tabBar.frame.origin.x = -2
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment