使用 UITabBarItem 外观居中选项卡标题

Posted

技术标签:

【中文标题】使用 UITabBarItem 外观居中选项卡标题【英文标题】:Centre the tab titles using UITabBarItem appearance 【发布时间】:2016-06-08 13:17:11 【问题描述】:

我正在使用以下代码突出显示选定的选项卡标题。

  [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName,nil] forState:UIControlStateNormal];

  [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor redColor], NSForegroundColorAttributeName, nil] forState:UIControlStateSelected];

上面的代码完美运行。我可以修改上面的代码来增加标签标题的字体大小并使其居中吗?

【问题讨论】:

你能张贴你想要得到的标签的截图吗? 现在标题位于标签的底部。我希望标题居中 ***.com/questions/10192665/… 【参考方案1】:

试试这个:

UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
tabBarController.selectedIndex = 1;
UITabBar *tabBar = tabBarController.tabBar;
UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];
UITabBarItem *tabBarItem3 = [tabBar.items objectAtIndex:3];
UITabBarItem *tabBarItem4 = [tabBar.items objectAtIndex:4];
tabBarItem1.title = @"Tab1"; tabBarItem2.title = @"Tab2";
tabBarItem3.title = @"Tab3"; tabBarItem4.title = @"Tab4";
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor whiteColor], NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
UIColor *titleHighlightedColor = [UIColor colorWithRed:153/255.0 green:192/255.0 blue:48/255.0 alpha:1.0];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: titleHighlightedColor, NSForegroundColorAttributeName, nil] forState:UIControlStateHighlighted]; return YES;

【讨论】:

【参考方案2】:

本准则将为您提供帮助。

// set the text color for selected state
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor], NSForegroundColorAttributeName,[UIFont fontWithName:YOUR_FONT_NAME size:YOUR_FONT_SIZE],NSFontAttributeName ,nil] forState:UIControlStateSelected];

// set the text color for unselected state
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor], NSForegroundColorAttributeName,[UIFont fontWithName:YOUR_FONT_NAME size:YOUR_FONT_SIZE],NSFontAttributeName ,nil] forState:UIControlStateNormal];


//To make title center use below one 
[[self.tabBarController.tabBar.items objectAtIndex:0] setTitlePositionAdjustment:UIOffsetMake(0, 0)];
[[self.tabBarController.tabBar.items objectAtIndex:1] setTitlePositionAdjustment:UIOffsetMake(0, 0)];

//if you have more tabs increase the index

【讨论】:

还是标题不居中兄弟 垂直移动标签,将偏移量设置为负值。 请参考这个答案:***.com/questions/10192665/… 想知道投反对票没有评论。投反对票的人应该发表评论。【参考方案3】:

这个解决方案适合我

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center
  
  if #available(ios 13, *) 
     let appearance = tabBarController.tabBar.standardAppearance
     appearance.stackedLayoutAppearance.normal.titleTextAttributes = [
        NSAttributedString.Key.paragraphStyle: paragraphStyle
     ]
     appearance.stackedLayoutAppearance.selected.titleTextAttributes = [
        NSAttributedString.Key.paragraphStyle: paragraphStyle
     ]
   else 
     if #available(iOS 11, *) 
        UITabBarItem.appearance().setTitleTextAttributes([
           NSAttributedString.Key.paragraphStyle: paragraphStyle
        ], for: .normal)
        UITabBarItem.appearance().setTitleTextAttributes([
           NSAttributedString.Key.paragraphStyle: paragraphStyle
        ], for: .selected)
     
  

【讨论】:

以上是关于使用 UITabBarItem 外观居中选项卡标题的主要内容,如果未能解决你的问题,请参考以下文章

UITabbarItem : - 下载的图像不会显示在选项卡项中。

如何改变UITabBarItem的宽度?

UITabBarItem 选定的选项卡背景:自定义?

UITabBarItem 图片不显示

自定义外观 QTabWidget

设置 UITabBarItem 徽章