16

有人可以告诉我如何将 UITabBarItem 的标题(例如 2px)移动到顶部吗?

4

5 回答 5

44

解决方案:

UITabBarItem *item = [tabBar.items objectAtIndex:0]; 
item.titlePositionAdjustment = UIOffsetMake(0, -5.0);
于 2013-03-10T11:25:57.383 回答
15

Swift 3 更新

将此代码放入UITabBarController

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

归功于蒂姆·布朗

于 2017-02-15T09:10:04.847 回答
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 
    } 
于 2018-03-24T07:35:41.320 回答
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++;
            }

        }
    }


}
于 2017-11-09T12:19:09.723 回答
-1

你不能......你也许可以破解它,但这并不容易,并且可能会在未来的 iOS 更新中破坏你的应用程序。

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

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

于 2012-05-04T11:42:52.013 回答