How to move title of UITabBarItem?

后端 未结 5 2114
长发绾君心
长发绾君心 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: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++;
                }
    
            }
        }
    
    
    }
    

提交回复
热议问题