有人可以告诉我如何将UITabBarItem的标题向上移动2像素吗?
有人可以告诉我如何将UITabBarItem的标题向上移动2像素吗?
解决方案:
UITabBarItem *item = [tabBar.items objectAtIndex:0];
item.titlePositionAdjustment = UIOffsetMake(0, -5.0);
Swift 3的更新
将此代码放入UITabBarController
中:
UITabBarItem.appearance().titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -5)
的贡献。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
}
您可以使用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++;
}
}
}
}
你不能这样做...虽然你可能能够通过黑客手段绕过它,但这并不容易,并且可能会在未来的iOS更新中破坏你的应用程序。
最好的方法是创建自己的UITabBar系统...这里有一个很好的指南供你参考...
http://idevrecipes.com/2011/01/04/how-does-the-twitter-iphone-app-implement-a-custom-tab-bar/
if ([item respondsToSelector:@selector(setTitlePositionAdjustment:)]) { item.titlePositionAdjustment = UIOffsetMake(0, -5.0); }
- liamnichols
item.setTitlePositionAdjustment(UIOffsetMake(0, -5.0))
(仅在XCode 6.1中提供setter函数) - average Joeitem.titlePositionAdjustment = UIOffset(horizontal:0,vertical:1000)
。 - Philipp Otto