Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/98.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用UITabBarItem外观将选项卡标题居中_Ios_Objective C_Uitabbar - Fatal编程技术网

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

Ios 使用UITabBarItem外观将选项卡标题居中,ios,objective-c,uitabbar,Ios,Objective C,Uitabbar,我使用以下代码突出显示所选选项卡标题 [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName,nil] forState:UIControlStateNormal]; [[UITabBarItem appearance] setTitleTextAttr

我使用以下代码突出显示所选选项卡标题

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

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

上面的代码工作得很好。我可以修改上述代码以增加选项卡标题的字体大小并使其居中吗?

此代码将帮助您

// 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
试试这个:

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;

标题仍然没有居中,如果要垂直移动标签,请将偏移量设置为负值。请参考以下答案:对没有评论的否决投票感到疑惑。反对票的人应该发表评论。你可以发布你想要的标签的截图吗?现在标题在标签的底部。我希望标题在中间