【发布时间】:2012-05-13 22:57:51
【问题描述】:
谁能告诉我如何将 UITabBarItem 的标题(例如 2px)移到顶部?
【问题讨论】:
标签: iphone objective-c xcode uitabbar uitabbaritem
谁能告诉我如何将 UITabBarItem 的标题(例如 2px)移到顶部?
【问题讨论】:
标签: iphone objective-c xcode uitabbar uitabbaritem
解决方案:
UITabBarItem *item = [tabBar.items objectAtIndex:0];
item.titlePositionAdjustment = UIOffsetMake(0, -5.0);
【讨论】:
item.setTitlePositionAdjustment(UIOffsetMake(0, -5.0))(XCode 6.1 中只有 setter 函数)
item.titlePositionAdjustment = UIOffset(horizontal:0,vertical:1000)
Swift 3 更新
将此代码放入UITabBarController:
UITabBarItem.appearance().titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -5)
归功于Tim Brown
【讨论】:
斯威夫特 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进行调整。
如果您想用它做更多事情,请访问 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++;
}
}
}
}
【讨论】:
你不能...你可以破解它,但这并不容易,并且可能会在未来的 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); }