How to remove spacing between tab bar items when using the "fill" value on the "item position" option?
I've tried the following:
let tabBarController = window!.rootViewController as! UITabBarController
tabBarController.tabBar.itemSpacing = 0
let numberOfItems = CGFloat(tabBarController.tabBar.items!.count)
let tabBarItemSize = CGSize(width: tabBarController.tabBar.frame.width / numberOfItems, height: tabBarController.tabBar.frame.height)
tabBarController.tabBar.selectionIndicatorImage = UIImage.imageWithColor(color: UIColor.secondaryHighlight(), size: tabBarItemSize).resizableImage(withCapInsets: UIEdgeInsets.zero)
for item in tabBarController.tabBar.items! {
item.imageInsets = UIEdgeInsetsMake(0, 0, 0, 0)
}
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
}
}
This result is always this (note the green line on the left of the first tab bar item):
Just by adding isScrollable: true parameter to TabBar() all tabs shrink to one side.
You can play with title offset that affects on image too:
tabBar.items!.first?.titlePositionAdjustment = UIOffsetMake(30.0, 0.0);
tabBar.items!.last?.titlePositionAdjustment = UIOffsetMake(-30.0, 0.0);
In this case result will be:
How it work with three items:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With