首页 文章

如何移动UITabBarItem的 Headers ?

提问于
浏览
13

有人可以告诉我,如何将UITabBarItem的 Headers 例如2px移到顶部?

5 回答

  • 5

    解:

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

    Swift 3的更新

    将此代码放在 UITabBarController 中:

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

    信用额度为Tim Brown

  • 0

    你不能......你可能会破解它的方式,但这并不容易,并可能在未来的iOS更新中打破你的应用程序 .

    最好的办法是创建自己的UITabBar系统...这是一个很好的指南,你可以看看..

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

  • 0

    您可以使用 tabbarItem.titlePositionAdjustment 进行调整 .

    如果你想用它做更多的事情,那么访问tabbaritem子视图并在 tabbar.subviews 上找到标签 . 例如

    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

    Swift 4和我一样,如果你在添加所有控制器之后在其他控制器内创建标签栏控制器,你可以循环选项卡栏项目并更改 Headers 和位置和字体

    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 
        }
    

相关问题