Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/109.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 未选择的阿巴尔颜色?_Ios_Uitabbarcontroller_Uitabbar_Uitabbaritem - Fatal编程技术网

Ios 未选择的阿巴尔颜色?

Ios 未选择的阿巴尔颜色?,ios,uitabbarcontroller,uitabbar,uitabbaritem,Ios,Uitabbarcontroller,Uitabbar,Uitabbaritem,我有一个有5个项目的UITabBar。我想更改所有项目的未选择颜色。这些项目没有在UIViewController类中声明(我构建了它们并在情节提要中链接了视图) 是否有这样的代码:[[UITabBar外观]设置***取消***选择图像颜色:[UIColor whiteColor]]?所以说我不能删除被接受的答案(我尝试过),但很明显,有很多评论认为这对iOS 7不起作用 请参阅下面的另一个答案以及更多的投票,或@Liam对该答案的评论中的链接 仅适用于iOS 6 它应该是这么简单: [[UI

我有一个有5个项目的UITabBar。我想更改所有项目的未选择颜色。这些项目没有在UIViewController类中声明(我构建了它们并在情节提要中链接了视图)


是否有这样的代码:
[[UITabBar外观]设置***取消***选择图像颜色:[UIColor whiteColor]]

所以说我不能删除被接受的答案(我尝试过),但很明显,有很多评论认为这对iOS 7不起作用

请参阅下面的另一个答案以及更多的投票,或@Liam对该答案的评论中的链接


仅适用于iOS 6

它应该是这么简单:

[[UITabBar appearance] setTintColor:[UIColor grayColor]]; // for unselected items that are gray
[[UITabBar appearance] setSelectedImageTintColor:[UIColor greenColor]]; // for selected items that are green

据我所知,这在iOS 7下不起作用。特别是,选项卡栏的tintColor将定义选定选项卡的颜色,而不是未选定选项卡的颜色。如果您想更改iOS 7中的默认设置,那么您似乎必须实际使用不同的图标(对于未选择的选项卡,使用您喜欢的颜色)并设置文本的颜色

此示例应将选定选项卡着色为红色,并将其他选项卡渲染为绿色。在TabBarController中运行以下代码:

// set color of selected icons and text to red
self.tabBar.tintColor = [UIColor redColor];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor redColor], NSForegroundColorAttributeName, nil] forState:UIControlStateSelected];


// set color of unselected text to green
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor greenColor], NSForegroundColorAttributeName, nil]
                                         forState:UIControlStateNormal];

// set selected and unselected icons
UITabBarItem *item0 = [self.tabBar.items objectAtIndex:0];

// this way, the icon gets rendered as it is (thus, it needs to be green in this example)
item0.image = [[UIImage imageNamed:@"unselected-icon.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

// this icon is used for selected tab and it will get tinted as defined in self.tabBar.tintColor
item0.selectedImage = [UIImage imageNamed:@"selected-icon.png"];
如果仅在故事板中设置图标,则只能控制所选选项卡的颜色(tintColor)。所有其他图标和相应文本将以灰色绘制


也许有人知道在iOS 7下采用颜色的更简单的方法?

参考下面的答案:

可以如下所示设置选定和未选定选项卡栏按钮的着色颜色:

[[UIView appearanceWhenContainedIn:[UITabBar class], nil] setTintColor:[UIColor redColor]];
[[UITabBar appearance] setSelectedImageTintColor:[UIColor greenColor]];
  if #available(iOS 13.0, *)
     {
        let appearance = tabBar.standardAppearance
        appearance.stackedLayoutAppearance.normal.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.black]
        appearance.stackedLayoutAppearance.selected.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.blue]
        appearance.stackedLayoutAppearance.normal.iconColor = UIColor.black
        appearance.stackedLayoutAppearance.selected.iconColor = UIColor.blue
        tabBar.standardAppearance = appearance
    } 
    else 
    {
        tabBar.unselectedItemTintColor = UIColor.black
        tabBar.tintColor = UIColor.blue
    }
第一行设置未选择的颜色(本例中为红色),方法是在选项卡栏中设置UIView的tintColor。请注意,这仅设置未选定图像的着色颜色-它不会更改其下方文本的颜色


第二行将选项卡栏的选定图像着色颜色设置为绿色。

扩展@Sven Tiffe针对iOS 7的答案,您可以让代码自动着色添加到情节提要中的未选定UITabBar图像。以下方法将使您无需创建两组图标图像(即选定和未选定)并以编程方式加载它们。将category方法imageWithColor:(请参见-)添加到项目中,然后将以下内容放入自定义UITabBarController viewDidLoad方法中:

// set the selected colors
[self.tabBar setTintColor:[UIColor whiteColor]];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor whiteColor], NSForegroundColorAttributeName, nil] forState:UIControlStateSelected];


UIColor * unselectedColor = [UIColor colorWithRed:184/255.0f green:224/255.0f blue:242/255.0f alpha:1.0f];

// set color of unselected text
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:unselectedColor, NSForegroundColorAttributeName, nil]
                                         forState:UIControlStateNormal];

// generate a tinted unselected image based on image passed via the storyboard
for(UITabBarItem *item in self.tabBar.items) {
    // use the UIImage category code for the imageWithColor: method
    item.image = [[item.selectedImage imageWithColor:unselectedColor] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}
在UIImage+Overlay.m上创建名为UIImage+Overlay的类别(从中提取):


将user3719695的答案转换为Swift,Swift现在使用扩展:

UIImage+Overlay.swift

extension UIImage {
  func imageWithColor(color1: UIColor) -> UIImage {
    UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
    color1.setFill()

    let context = UIGraphicsGetCurrentContext()
    CGContextTranslateCTM(context, 0, self.size.height)
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextSetBlendMode(context, CGBlendMode.Normal)

    let rect = CGRectMake(0, 0, self.size.width, self.size.height) as CGRect
    CGContextClipToMask(context, rect, self.CGImage)
    CGContextFillRect(context, rect)

    let newImage = UIGraphicsGetImageFromCurrentImageContext() as UIImage
    UIGraphicsEndImageContext()

    return newImage
  }
}
override func viewDidLoad() {
  super.viewDidLoad()
  for item in self.tabBar.items! {
    item.image = item.selectedImage?.imageWithColor(unselectedColor).imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
    //In case you wish to change the font color as well
    let attributes = [NSForegroundColorAttributeName: unselectedColor]
    item.setTitleTextAttributes(attributes, forState: UIControlState.Normal)
  }
}
customTabBar.swift

extension UIImage {
  func imageWithColor(color1: UIColor) -> UIImage {
    UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
    color1.setFill()

    let context = UIGraphicsGetCurrentContext()
    CGContextTranslateCTM(context, 0, self.size.height)
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextSetBlendMode(context, CGBlendMode.Normal)

    let rect = CGRectMake(0, 0, self.size.width, self.size.height) as CGRect
    CGContextClipToMask(context, rect, self.CGImage)
    CGContextFillRect(context, rect)

    let newImage = UIGraphicsGetImageFromCurrentImageContext() as UIImage
    UIGraphicsEndImageContext()

    return newImage
  }
}
override func viewDidLoad() {
  super.viewDidLoad()
  for item in self.tabBar.items! {
    item.image = item.selectedImage?.imageWithColor(unselectedColor).imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
    //In case you wish to change the font color as well
    let attributes = [NSForegroundColorAttributeName: unselectedColor]
    item.setTitleTextAttributes(attributes, forState: UIControlState.Normal)
  }
}

我不得不将代码移动到
viewwillbeen
中,因为在
viewDidLoad
中图像尚未设置

Swift 4翻译

import Foundation
import UIKit

extension UIImage {
    func with(color: UIColor) -> UIImage {
        guard let cgImage = self.cgImage else {
            return self
        }
        UIGraphicsBeginImageContextWithOptions(size, false, scale)
        let context = UIGraphicsGetCurrentContext()!
        context.translateBy(x: 0, y: size.height)
        context.scaleBy(x: 1.0, y: -1.0)
        context.setBlendMode(.normal)
        let imageRect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
        context.clip(to: imageRect, mask: cgImage)
        color.setFill()
        context.fill(imageRect)
        let newImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext();
        return newImage
    }
}

class MYTabBarController: UITabBarController {

    let unselectedColor = UIColor(red: 108/255.0, green: 110/255.0, blue: 114/255.0, alpha: 1.0)
    let selectedColor = UIColor.blue()

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        // Unselected state colors
        for item in self.tabBar.items! {
            item.image = item.selectedImage!.with(color: unselectedColor).withRenderingMode(.alwaysOriginal)
        }
        UITabBarItem.appearance().setTitleTextAttributes([.foregroundColor : unselectedColor], for: .normal)

        // Selected state colors
        tabBar.tintColor = selectedColor
        UITabBarItem.appearance().setTitleTextAttributes([.foregroundColor : selectedColor], for: .selected)
    }
}

@JoeGalid的
imageWithColor:
含Xamarin的溶液:

using CoreGraphics;
using UIKit;

namespace Example
{
    public static class UIImageExtensions
    {
        public static UIImage ImageWithColor(this UIImage image, UIColor color)
        {
            UIGraphics.BeginImageContextWithOptions(image.Size, false, image.CurrentScale);

            color.SetFill();

            var context = UIGraphics.GetCurrentContext();

            context.TranslateCTM(0, image.Size.Height);
            context.ScaleCTM(1.0f, -1.0f);
            context.SetBlendMode(CoreGraphics.CGBlendMode.Normal);

            var rect = new CGRect(0, 0, image.Size.Width, image.Size.Height);
            context.ClipToMask(rect, image.CGImage);
            context.FillRect(rect);

            var newImage = UIGraphics.GetImageFromCurrentImageContext() as UIImage;
            UIGraphics.EndImageContext();

            return newImage;
        }
    }
}
然后在设置选项卡栏项目时使用它:

var image = UIImage.FromBundle("name");
barItem.Image = image.ImageWithColor(UIColor.Gray).ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal);
barItem.SelectedImage = image.ImageWithColor(UIColor.Red).ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)

在iOS 10及更高版本中,有3种可能的简单解决方案:

A.代码实例(Swift):

B.来自IB的实例:

添加类型为:
Color

C.全球外观(Swift):


从iOS 10+开始,通过编程实现这一点的新方法是使用API。例如,如果您已在
AppDelegate
中初始化选项卡栏控制器,则如下所示:

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        ...

        let firstViewController = VC1()
        let secondViewController = VC2()
        let thirdViewController = VC3()


        let tabBarCtrl = UITabBarController()
        tabBarCtrl.viewControllers = [firstViewController, secondViewController, thirdViewController]

        // set the color of the active tab
        tabBarCtrl.tabBar.tintColor = UIColor.white

        // set the color of the inactive tabs
        tabBarCtrl.tabBar.unselectedItemTintColor = UIColor.gray

        ...
    }

iOS 10及更高版本中的Swift版本-

UITabBar.appearance().tintColor = UIColor.gray
UITabBar.appearance().unselectedItemTintColor = UIColor.gray
Swift 4版本(无隐式展开选项):

UIImage+Overlay.swift

import UIKit

extension UIImage {
    func with(color: UIColor) -> UIImage? {
        guard let cgImage = self.cgImage else {
            return self
        }
        UIGraphicsBeginImageContextWithOptions(size, false, scale)
        if let context = UIGraphicsGetCurrentContext() {
            context.translateBy(x: 0, y: size.height)
            context.scaleBy(x: 1.0, y: -1.0)
            context.setBlendMode(.normal)
            let imageRect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
            context.clip(to: imageRect, mask: cgImage)
            color.setFill()
            context.fill(imageRect)
            if let newImage = UIGraphicsGetImageFromCurrentImageContext() {
                UIGraphicsEndImageContext();
                return newImage
            }
        }
        return nil;
    }
}
class CustomTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        if #available(iOS 10.0, *) {
            self.tabBar.unselectedItemTintColor = UIColor.init(white: 1, alpha: 0.5)
        } else {
            // Fallback on earlier versions
            if let items = self.tabBar.items {
                let unselectedColor = UIColor.init(white: 1, alpha: 0.5)
                let selectedColor = UIColor.white
                // Unselected state colors
                for item in items {
                    if let selectedImage = item.selectedImage?.with(color: unselectedColor)?.withRenderingMode(.alwaysOriginal) {
                        item.image = selectedImage
                    }
                }
                UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor : unselectedColor], for: .normal)

                // Selected state colors
                tabBar.tintColor = selectedColor
                UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor : selectedColor], for: .selected)
            }
        }

        UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "overpass-light", size: 12)!, NSAttributedStringKey.foregroundColor: UIColor.white], for: UIControlState.normal)
    }
}

CustomTabBarController.swift

import UIKit

extension UIImage {
    func with(color: UIColor) -> UIImage? {
        guard let cgImage = self.cgImage else {
            return self
        }
        UIGraphicsBeginImageContextWithOptions(size, false, scale)
        if let context = UIGraphicsGetCurrentContext() {
            context.translateBy(x: 0, y: size.height)
            context.scaleBy(x: 1.0, y: -1.0)
            context.setBlendMode(.normal)
            let imageRect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
            context.clip(to: imageRect, mask: cgImage)
            color.setFill()
            context.fill(imageRect)
            if let newImage = UIGraphicsGetImageFromCurrentImageContext() {
                UIGraphicsEndImageContext();
                return newImage
            }
        }
        return nil;
    }
}
class CustomTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        if #available(iOS 10.0, *) {
            self.tabBar.unselectedItemTintColor = UIColor.init(white: 1, alpha: 0.5)
        } else {
            // Fallback on earlier versions
            if let items = self.tabBar.items {
                let unselectedColor = UIColor.init(white: 1, alpha: 0.5)
                let selectedColor = UIColor.white
                // Unselected state colors
                for item in items {
                    if let selectedImage = item.selectedImage?.with(color: unselectedColor)?.withRenderingMode(.alwaysOriginal) {
                        item.image = selectedImage
                    }
                }
                UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor : unselectedColor], for: .normal)

                // Selected state colors
                tabBar.tintColor = selectedColor
                UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor : selectedColor], for: .selected)
            }
        }

        UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "overpass-light", size: 12)!, NSAttributedStringKey.foregroundColor: UIColor.white], for: UIControlState.normal)
    }
}
或者只是没有编码Swift 4Xcode 10.1

  • 使用界面生成器在视图控制器上添加
    uitabar
  • 在左侧面板中选择添加的视图
  • 键入
    cmd+alt+3
    或只需单击右面板中的
    显示身份检查器
  • 用户定义的运行时属性部分
    中,单击加上按钮添加一个新属性,并将其称为
    未选择的EditemTintColor
    (请参阅)
  • 在不离开上一步的部分(参见编号4)的情况下,在
    类型
    列下选择
    颜色
    类型
  • 最后,在
    Value
    部分下设置必要的颜色
  • 编译您的项目

  • 结束。祝贺你 使用swift未选择选项卡栏的颜色

  • 获取TabBarViewController的引用
  • 使用以下代码

       [You tabbar controller name]?.tabBar.unselectedItemTintColor = [color name here]
    

  • 希望能有所帮助。

    iOS 13中有一个新的外观API。要使用Xcode 11.0为选项卡项的图标和文本正确上色,可以如下使用:

    [[UIView appearanceWhenContainedIn:[UITabBar class], nil] setTintColor:[UIColor redColor]];
    [[UITabBar appearance] setSelectedImageTintColor:[UIColor greenColor]];
    
      if #available(iOS 13.0, *)
         {
            let appearance = tabBar.standardAppearance
            appearance.stackedLayoutAppearance.normal.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.black]
            appearance.stackedLayoutAppearance.selected.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.blue]
            appearance.stackedLayoutAppearance.normal.iconColor = UIColor.black
            appearance.stackedLayoutAppearance.selected.iconColor = UIColor.blue
            tabBar.standardAppearance = appearance
        } 
        else 
        {
            tabBar.unselectedItemTintColor = UIColor.black
            tabBar.tintColor = UIColor.blue
        }
    

    注意—“UITextAttributeTextColor”已被弃用:首次在iOS 7.0中弃用-使用NSForegroundColorAttributeName:)这是更改图标ColorsOry@Atma的唯一解决方案,另外还有一个更简单的解决方案,用户3719695在6月发布。加上[UItabaritem外观]的1保存了我的一天谢谢:)但是有没有什么方法我们不需要为tabbar按钮设置单独的图像?完美的解决方案我的问题你可以在iOS 7上检查这个answare:这个可能的副本在iOS 7中不起作用。这一点对我很有用。对于iOS 10,使用这个
    self.dashboardTabBar.unselectedItemTintColor=UIColor.black
    我会得到同样的结果。只有第一次。未选择的颜色有效,但在选择后它会切换回正常颜色,至少在7.1上是这样。这是迄今为止完成任务最简单的方法,不知道为什么它没有被标记为可接受的答案。在我的情况下,将上述代码添加到ViewWillAspect中就成功了。(在我的应用程序中调用viewDidLoad时,按钮还没有加载,因此上面的代码仅在放置在viewDidLoad中时更改文本颜色)。我认为应该是
    item.image=[[item.image imageWithColor…]
    。另外,将
    imageWithColor
    的代码也包括在内,这将改进您的答案