29

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?

Srikar Appalaraju
  • 66,073
  • 51
  • 206
  • 260
user4234
  • 1,763
  • 1
  • 19
  • 33

6 Answers6

53

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

Community
  • 1
  • 1
Srikar Appalaraju
  • 66,073
  • 51
  • 206
  • 260
43

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)
user3378170
  • 2,191
  • 19
  • 11
Tim Windsor Brown
  • 3,853
  • 5
  • 23
  • 33
  • correction for Swift: `UITabBarItem.appearance().titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -4)` – 262Hz Dec 30 '15 at 01: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 '16 at 16:32
11

Here's the same trick with Interface builder:

enter image description here

AmitP
  • 4,985
  • 4
  • 31
  • 27
10

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

Aurora0001
  • 10,827
  • 5
  • 47
  • 50
kamil3
  • 1,072
  • 1
  • 13
  • 18
  • 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 08 '18 at 10:21
2

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) 
     }
Yogendra Singh
  • 1,513
  • 20
  • 15
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.

Community
  • 1
  • 1
user4234
  • 1,763
  • 1
  • 19
  • 33