更改UITabBar图标的颜色

问题描述 投票:0回答:4

我想将 UI 选项卡栏图标颜色更改为以下值:红色:0.75,绿色:1.0,蓝色:1.0,Alpha:1.0,未选择图标时更改为白色。

直到现在我还以为你是这样做的:

UITabBarItem.appearance().finishedSelectedImage([NSForegroundColorAttributeName: UIColor(red: 0.75, green: 1.0, blue: 1.0, alpha: 1.0)], forState: UIControlState.Selected)

    UITabBarItem.appearance().finishedUnselectedImage([NSForegroundColorAttributeName: UIColor.whiteColor()], forState: UIControlState.Normal)

上面的代码在我的委托中。

现在我已经输入了这段代码:

import UIKit

class UITabBarViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    var testImage = UIImage(named: "22274")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
    testImageView.tintColor = UIColor.redColor()
    testImageView.image = testImage

    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}



}

进入我的标签栏查看控制器 cocco touch 文件。我不知道为什么我仍然收到错误

ios swift uitabbarcontroller uitabbar uicolor
4个回答
0
投票

我遍历了UITabbarController的所有子控制器,然后设置每个控制器的TabBarItem的颜色或色调颜色,它确实有效。


0
投票

如果 tabbaritem 有图像,您可以通过以下方式设置其颜色:

var testImage = UIImage(named: "someImageName")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
testImageView.tintColor = UIColor.redColor()
testImageView.image = testImage

0
投票

我认为它无法制作,您可以获取所需颜色的图像并在 UITabbar 中使用它 但要更改所有 TabBar 颜色,您可以在 Func 的 AppDelegate 中使用以下内容:

didFinishLaunchingWithOptions

待添加线路:

UITabBar.appearance().barTintColor = #colorLiteral(red: 0.2000651062, green: 0.1960035861, blue: 0.2000851929, alpha: 1)

UITabBar.appearance().tintColor = #colorLiteral(red: 0.2000651062, green: 0.1960035861, blue: 0.2000851929, alpha: 1)

UITabBar.appearance().unselectedItemTintColor = #colorLiteral(red: 0.7415059209, green: 0.5448099971, blue: 0.5051562786, alpha: 1) 

注意:您可以根据您的应用程序更改颜色

查看图片:https://i.stack.imgur.com/wESVH.png

祝您编码日快乐


0
投票

受到@Menaim的启发,我在AppDelegate的代码库中找到了解决方案。将窗口?.tintColor 更改为所需的颜色。

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

      func application(_ application: UIApplication, didFinishLaunchingWithOptions 
                                                                      launchOptions:
           [UIApplication.LaunchOptionsKey: Any]? -> Bool
           window?.tintColor = .systemOrange

}
      
© www.soinside.com 2019 - 2024. All rights reserved.