F.SO7
F.SO7

Reputation: 935

Align UITabBarItem image to the left and to the right

I have a UITabBar with 3 items.

Each item has just an image (without a text).

What happens now is that all of the three images are presented in the center of each `tabBarItem.

What I want to accomplish is that the left UITabBarItem's image will be aligned to the left. The middle UITabBarItem's image will stay in the center, and the right UITabBarItem will be aligned to the right.

I've searched all over the internet a way, but UITabBarItem doesn't have any alignImage property or something like that.

Any idea how can I do that?

Thank you!

Upvotes: 2

Views: 3648

Answers (3)

Santosh
Santosh

Reputation: 2914

Can you try this? Basically you could set the insets of the image property on your tabBarButton(UITabBarItem). There could be another way but give a try.

let tabBarButton = UITabBarItem.init(title: "One", image: myImage, selectedImage: nil)
tabBarButton.image?.withAlignmentRectInsets(UIEdgeInsetsMake(0, 15, 0, 0))//Give your left alignment number

//one more way
tabBarButton.imageInsets = UIEdgeInsetsMake(0, 15, 0, 0)

Upvotes: 0

Rahul Kumar
Rahul Kumar

Reputation: 3157

You can do this by specifying horizontal value of "Custom offset" for UITabBarItem in Storyboard as per screenshot. (Note: give positive value for right TabBarItem to the right, negative value for left one and "Default position" for center one)

enter image description here

In device, it will look like as below:

enter image description here

Upvotes: 0

Nishant Bhindi
Nishant Bhindi

Reputation: 2252

enter image description here

Set image inset of tab bar item like this

Upvotes: 1

Related Questions