如何移动UITabBarItem的标题?

16

有人可以告诉我如何将UITabBarItem的标题向上移动2像素吗?

5个回答

44

解决方案:

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

1
令人惊讶的是,在Swift中:item.setTitlePositionAdjustment(UIOffsetMake(0, -5.0))(仅在XCode 6.1中提供setter函数) - average Joe
4
在Swift2/Xcode7中,setter函数已经消失了,您可以使用普通的属性来设置值。item.titlePositionAdjustment = UIOffset(horizontal:0,vertical:1000) - Philipp Otto

15

Swift 3的更新

将此代码放入UITabBarController中:

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

感谢Tim Brown

的贡献。

1

Swift 4 如果您像我一样,在其他控制器中创建选项卡控制器,只需在添加所有控制器后循环遍历选项卡项并更改标题、位置和字体即可。

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

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

如果您想要对其进行更多操作,则可以访问tabbar.subviews中的标签来访问tabbaritem子视图。例如:

 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++;
            }

        }
    }


}

-1

3
我找到了这个解决方案: UITabBarItem *item = [tabBar.items objectAtIndex:0]; item.titlePositionAdjustment = UIOffsetMake(0, -5.0);翻译:我发现了这个解决方案: UITabBarItem *item = [tabBar.items objectAtIndex:0]; item.titlePositionAdjustment = UIOffsetMake(0,-5.0); - iWizard
3
抱歉,上次我查找此功能时并没有发现它... 现在正在阅读该类的参考资料,发现只适用于iOS5及以上版本,因此如果您的应用程序既要在iOS 5之前又要在iOS 5之后运行,请在调用函数之前检查该功能是否可用... 您可以这样做... if ([item respondsToSelector:@selector(setTitlePositionAdjustment:)]) { item.titlePositionAdjustment = UIOffsetMake(0, -5.0); } - liamnichols

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接