user3427013
user3427013

Reputation: 1099

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

Upvotes: 6

Views: 4205

Answers (4)

Lobna
Lobna

Reputation: 11

to be able to control the location of all tab bar items "not only the first and last" you can use the items array index like this:

tabBar.items?[1].titlePositionAdjustment = UIOffsetMake(-15.0 , 0.0) tabBar.items?[2].titlePositionAdjustment = UIOffsetMake(15.0 , 0.0)

Upvotes: 1

Artem
Artem

Reputation: 401

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

Upvotes: 4

Aaryam Maheshwary
Aaryam Maheshwary

Reputation: 3

Sorry for the late answer. Hope this helps!

tabBar.frame.size.width = self.view.frame.width + 4
tabBar.frame.origin.x = -2

Upvotes: 0

Valeriy
Valeriy

Reputation: 743

    tabBarController?.tabBar.frame.origin.x = -2
    tabBarController?.tabBar.frame.size.width += 2

Upvotes: 0

Related Questions