在“更多”菜单中更改UITabBar色调颜色

时间:2020-12-13 00:12:07

I'm trying to change the blue colour from icons in the more menu. I tried almost everything I found on Stack Overflow, but nothing worked. I tried this solution, but is not working.

我正在尝试从更多菜单中的图标更改蓝色。我尝试了几乎所有我在Stack Overflow上找到的东西,但没有任何效果。我试过这个解决方案,但是没有用。

The only option I found to change the colour was

我发现改变颜色的唯一选择是

[[UIView appearance] setTintColor:[UIColor redColor]];

but it changes all colours in the app.

但它改变了应用程序中的所有颜色。

在“更多”菜单中更改UITabBar色调颜色在“更多”菜单中更改UITabBar色调颜色

The code is just a new project with storyboard, so I just added the views on the storyboard.
Thanks for helping.

代码只是一个带有故事板的新项目,所以我只是在故事板上添加了视图。谢谢你的帮助。

Edit: After I added the code:

编辑:我添加代码后:

    UIImage *myImage = [[UIImage imageNamed:@"music.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
self.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"New Title" image:myImage selectedImage:[UIImage imageNamed:@"music.png"]];

The image is changed when the view is selected, but it's still blue.

选择视图时图像会发生变化,但仍然是蓝色。

2 个解决方案

#1


7  

To do what you need, you should use images by creating UITabBarItem for each controller and add an image and a selected image.

要做你需要的,你应该通过为每个控制器创建UITabBarItem来使用图像,并添加图像和选定的图像。

See Apple Documentation about UITabBarItem

请参阅有关UITabBarItem的Apple文档

Otherwise looks here, from @Aaron Brager :

从@Aaron Brager看其他地方:

Edit after seing the full code First there is many mistakes in your project, assets should be in xcassets folder, in view didload write your code after the 'super viewDidLoad]', etc.

在查看完整代码后进行编辑首先,你的项目中存在很多错误,资产应该在xcassets文件夹中,在视图中,在'super viewDidLoad'之后写入你的代码,等等。

About your problem, in your viewDidLoad method in the FirstViewController

关于您的问题,在FirstViewController的viewDidLoad方法中

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    // Your code start here, not before the super
    [[UITabBar appearance] setTintColor:[UIColor redColor]];

    // Get table view of more new viewController
    UITableView *view =(UITableView*)self.tabBarController.moreNavigationController.topViewController.view;

    view.tintColor = [UIColor redColor]; // Change the image color

    if ([[view subviews] count]) {
        for (UITableViewCell *cell in [view visibleCells]) {
            cell.textLabel.textColor = [UIColor redColor]; // Change the text color

        }
    }
}

#2


0  

This is the Swift version of Ludovic's answer.

这是Ludovic答案的Swift版本。

Keep in mind that this version only changes the tint color, since the original answer did the text color change in a very hacky way. For changing it properly, you'd have to override moreNavigationController and its cellForRowAt function.

请记住,此版本仅更改色调颜色,因为原始答案以非常黑客的方式更改了文本颜色。要正确更改它,您必须覆盖moreNavigationController及其cellForRowAt函数。

tabBarController?.tabBar.tintColor = .red

if let moreTableView = tabBarController?.moreNavigationController.topViewController?.view as? UITableView {
    moreTableView.tintColor = .red
}

#1


7  

To do what you need, you should use images by creating UITabBarItem for each controller and add an image and a selected image.

要做你需要的,你应该通过为每个控制器创建UITabBarItem来使用图像,并添加图像和选定的图像。

See Apple Documentation about UITabBarItem

请参阅有关UITabBarItem的Apple文档

Otherwise looks here, from @Aaron Brager :

从@Aaron Brager看其他地方:

Edit after seing the full code First there is many mistakes in your project, assets should be in xcassets folder, in view didload write your code after the 'super viewDidLoad]', etc.

在查看完整代码后进行编辑首先,你的项目中存在很多错误,资产应该在xcassets文件夹中,在视图中,在'super viewDidLoad'之后写入你的代码,等等。

About your problem, in your viewDidLoad method in the FirstViewController

关于您的问题,在FirstViewController的viewDidLoad方法中

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    // Your code start here, not before the super
    [[UITabBar appearance] setTintColor:[UIColor redColor]];

    // Get table view of more new viewController
    UITableView *view =(UITableView*)self.tabBarController.moreNavigationController.topViewController.view;

    view.tintColor = [UIColor redColor]; // Change the image color

    if ([[view subviews] count]) {
        for (UITableViewCell *cell in [view visibleCells]) {
            cell.textLabel.textColor = [UIColor redColor]; // Change the text color

        }
    }
}

#2


0  

This is the Swift version of Ludovic's answer.

这是Ludovic答案的Swift版本。

Keep in mind that this version only changes the tint color, since the original answer did the text color change in a very hacky way. For changing it properly, you'd have to override moreNavigationController and its cellForRowAt function.

请记住,此版本仅更改色调颜色,因为原始答案以非常黑客的方式更改了文本颜色。要正确更改它,您必须覆盖moreNavigationController及其cellForRowAt函数。

tabBarController?.tabBar.tintColor = .red

if let moreTableView = tabBarController?.moreNavigationController.topViewController?.view as? UITableView {
    moreTableView.tintColor = .red
}