ios 有没有办法更改 UITabBar 或 UITabBarItem 中的文本位置?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13618268/
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
Is there a way to change the text position in UITabBar or UITabBarItem?
提问by Sharen Eayrs
This is a custom tab bar I intended to put up on the screen. However, my partner want the text to be slightly up. How can I do so?
这是我打算放在屏幕上的自定义标签栏。但是,我的搭档希望文本稍微向上。我怎么能这样做?
回答by Srikar Appalaraju
Why don't you just have an empty title property for your view controller and add the title to your custom images for the tab?
为什么不为视图控制器设置一个空的标题属性并将标题添加到选项卡的自定义图像中?
UPDATE:For the sake of completeness of answer; from comments and ios tabbar put text in the middle when no image
更新:为了答案的完整性;当没有图像时,来自评论和ios tabbar 将文本放在中间
[tab.tabBarItem setTitlePositionAdjustment:UIOffsetMake(0, -10)]
[tab.tabBarItem setTitlePositionAdjustment:UIOffsetMake(0, -10)]
回答by Tim Windsor Brown
You probably want to apply this offset globally, so I'd suggest
你可能想在全球范围内应用这个偏移量,所以我建议
Obj-C
对象-C
[UITabBarItem appearance].titlePositionAdjustment = UIOffsetMake(0, -4);
Swift:
迅速:
UITabBarItem.appearance().titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -4)
回答by kamil3
You can do it directly from Interface Builder (Custom Offset in Title Position), like this:
您可以直接从 Interface Builder(标题位置的自定义偏移)中执行此操作,如下所示:
回答by Yogendra Singh
For iOS 13 and later:
对于 iOS 13 及更高版本:
if #available(iOS 13, *) {
let appearance = UITabBarAppearance()
appearance.stackedLayoutAppearance.selected.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -10)
appearance.stackedLayoutAppearance.normal.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -10)
}
回答by Sharen Eayrs
I've found the answer here: ios tabbar put text in the middle when no imagewill try this one and see how it goes :)
我在这里找到了答案:ios tabbar 将文本放在中间时没有图像会尝试这个,看看它是如何进行的:)
Update: It works. Only 1 line of code.
更新:它有效。只有 1 行代码。