如何移动UITabBarItem的标题?

时间:2012-05-04 11:39:19

标签: iphone objective-c xcode uitabbar uitabbaritem

有人可以告诉我,我怎样才能将UITabBarItem的标题例如2px移到顶部?

4 个答案:

答案 0 :(得分:8)

  

Swift 3的更新

将此代码放在UITabBarController

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

信用转到Tim Brown

答案 1 :(得分:1)

斯威夫特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 
    } 

答案 2 :(得分:0)

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

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

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

答案 3 :(得分: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++;
            }

        }
    }


}