Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove spacing between tab bar items (left and right)

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):

tab bar items

like image 798
user3427013 Avatar asked Nov 11 '16 22:11

user3427013


People also ask

How do you get rid of the space between two tabs in flutter?

Just by adding isScrollable: true parameter to TabBar() all tabs shrink to one side.


1 Answers

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:
result screenshot

How it work with three items:
three items result screenshot

like image 180
Artem Avatar answered Oct 05 '22 13:10

Artem