xcode 如何移动 UITabBarItem 的标题?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10448130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:11:30  来源:igfitidea点击:

How to move title of UITabBarItem?

iphoneobjective-cxcodeuitabbaruitabbaritem

提问by iWizard

Can somebody tell me please how can I move title of UITabBarItem for example 2px to top?

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

回答by iWizard

Solution:

解决方案:

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

回答by Ibrahim

Update for Swift 3

Swift 3 更新

Put this code inside UITabBarController:

把这段代码放在里面UITabBarController

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

Credit goes to Tim Brown

归功于蒂姆·布朗

回答by Iraniya Naynesh

Swift 4 Like me, if you are creating tab bar controller inside some other controller just after adding all controller you can loop through the tab bar items and change title and positioning and font

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 
    } 

回答by Abeer Iqbal

You can use tabbarItem.titlePositionAdjustmentto adjust.

可以tabbarItem.titlePositionAdjustment用来调整。

In case you want to do much more with that then access tabbaritem subview and find label on tabbar.subviews. For example

如果您想用它做更多事情,请访问 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++;
            }

        }
    }


}

回答by liamnichols

You can't... you might be able to hack your way around it but that wouldn't be easy and could break your app on future iOS updates.

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

Best bet would be to create your own UITabBar system... heres a good guide you could take a look at..

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

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

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