Can somebody tell me please how can I move title of UITabBarItem for example 2px to top?
Solution:
UITabBarItem *item = [tabBar.items objectAtIndex:0];
item.titlePositionAdjustment = UIOffsetMake(0, -5.0);
Update for Swift 3
Put this code inside UITabBarController
:
UITabBarItem.appearance().titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -5)
Credit goes to Tim Brown
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
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
}
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.
Best bet would be to create your own UITabBar system... heres a good guide you could take a look at..
http://idevrecipes.com/2011/01/04/how-does-the-twitter-iphone-app-implement-a-custom-tab-bar/
You can use tabbarItem.titlePositionAdjustment
to adjust.
In case you want to do much more with that then access tabbaritem subview and find label on tabbar.subviews
. For example
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++;
}
}
}
}
来源:https://stackoverflow.com/questions/10448130/how-to-move-title-of-uitabbaritem