How to move title of UITabBarItem?

半城伤御伤魂 提交于 2019-12-03 10:56:05

Solution:

UITabBarItem *item = [tabBar.items objectAtIndex:0]; 
item.titlePositionAdjustment = UIOffsetMake(0, -5.0);
Ibrahim

Update for Swift 3

Put this code inside UITabBarController:

UITabBarItem.appearance().titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -5)

Credit goes to Tim Brown

Swift 4 Like me, if you are creating tab bar controller inside some other controller just after adding all controller you can loop through the tab bar items and change title and positioning and font

for (index,tabBarItem) in tabBarController.tabBar.items!.enumerated() {
        tabBarItem.image = nil //if you only want title and no Image
        tabBarItem.title = titleArray[index] //setting your title based on some array
        let attributes = [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 20)] // changeing font and height of the text
        tabBarItem.setTitleTextAttributes(attributes, for: .normal)
        tabBarItem.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -10.0) // updating the title positon 
    } 

You can't... you might be able to hack your way around it but that wouldn't be easy and could break your app on future iOS updates.

Best bet would be to create your own UITabBar system... heres a good guide you could take a look at..

http://idevrecipes.com/2011/01/04/how-does-the-twitter-iphone-app-implement-a-custom-tab-bar/

You can use tabbarItem.titlePositionAdjustment to adjust.

In case you want to do much more with that then access tabbaritem subview and find label on tabbar.subviews. For example

 int i = 0;
for (NSObject *view in self.tabBar.subviews)
{
    MTLog(@"%@", view);
    if ([view respondsToSelector:@selector(subviews)])
    {
        for (NSObject *childView in ((UIView *)view).subviews)
        {
            if ([childView isKindOfClass:[UILabel class]])
            {
                if (i > (titlesArray.count - 1))
                    break;
                UILabel *label = (UILabel *)childView;
                NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:titlesArray[i] attributes:@{
                                                                                                                                            NSFontAttributeName:[UIFont systemFontOfSize:9]
                                                                                                                                            }];

                NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
                [style setMinimumLineHeight:26];
                [style setAlignment:NSTextAlignmentRight];

                [attributedString addAttribute:NSParagraphStyleAttributeName
                                         value:style
                                         range:NSMakeRange(0, attributedString.length)];
                label.attributedText = attributedString;
                i++;
            }

        }
    }


}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!