How to move title of UITabBarItem?

后端 未结 5 2098
长发绾君心
长发绾君心 2021-02-13 04:08

Can somebody tell me please how can I move title of UITabBarItem for example 2px to top?

相关标签:
5条回答
  • 2021-02-13 04:34

    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/

    0 讨论(0)
  • 2021-02-13 04:42

    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++;
                }
    
            }
        }
    
    
    }
    
    0 讨论(0)
  • 2021-02-13 04:43

    Update for Swift 3

    Put this code inside UITabBarController:

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

    Credit goes to Tim Brown

    0 讨论(0)
  • 2021-02-13 04:46

    Solution:

    UITabBarItem *item = [tabBar.items objectAtIndex:0]; 
    item.titlePositionAdjustment = UIOffsetMake(0, -5.0);
    
    0 讨论(0)
  • 2021-02-13 04:47

    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 
        } 
    
    0 讨论(0)
提交回复
热议问题