31

enter image description here

This is a custom tab bar I intended to put up on the screen. However, my partner want the text to be slightly up. How can I do so?

6 Answers 6

54

Why don't you just have an empty title property for your view controller and add the title to your custom images for the tab?

UPDATE: For the sake of completeness of answer; from comments and ios tabbar put text in the middle when no image

[tab.tabBarItem setTitlePositionAdjustment:UIOffsetMake(0, -10)]

5
  • Good enough. I found a better way:stackoverflow.com/questions/12586411/…
    – user4234
    Nov 29, 2012 at 3:28
  • ended up selecting your answers because it's more comprehensive :)
    – user4234
    Dec 3, 2012 at 5:11
  • Why (0, -10) ? I don't understand... Can you explain me?
    – LKM
    Aug 8, 2015 at 15:04
  • @LKM 0 means no horizontal offset, -10 moves the text up. Aug 19, 2015 at 23:49
  • You also can do it in storyboard.
    – Nike Kov
    Apr 28, 2017 at 19:16
46

You probably want to apply this offset globally, so I'd suggest

Obj-C

[UITabBarItem appearance].titlePositionAdjustment = UIOffsetMake(0, -4);

Swift:

UITabBarItem.appearance().titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -4)
2
  • correction for Swift: UITabBarItem.appearance().titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -4)
    – 262Hz
    Dec 30, 2015 at 1:44
  • 3
    If you just want to adjust the vertical position (in swift) you might also use this UITabBarItem.appearance().titlePositionAdjustment.vertical = -4
    – benrudhart
    Dec 12, 2016 at 16:32
11

Here's the same trick with Interface builder:

enter image description here

1
  • Nice one! Also introduces the user defined runtime attributes section, thanks!
    – craft
    Apr 19, 2019 at 17:55
10

You can do it directly from Interface Builder (Custom Offset in Title Position), like this: link

1
  • 1
    Just tried it and it works, however, when you go into landscape mode, your text will not be centered to your image anymore (because in landscape mode image and label are positioned horizontaly, not vertically)
    – Starwave
    Mar 8, 2018 at 10:21
7

For iOS 13 and later:

if #available(iOS 13, *) {
        let appearance = UITabBarAppearance()
           appearance.stackedLayoutAppearance.selected.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -10)
           appearance.stackedLayoutAppearance.normal.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -10) 
     }
0
0

I've found the answer here: ios tabbar put text in the middle when no image will try this one and see how it goes :)

Update: It works. Only 1 line of code.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.