当前位置: 首页 > 知识库问答 >
问题:

未选择的UITabBar颜色?

糜博远
2023-03-14

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

是否有这样的代码:[[UITabBar外观]set***UN***SseltedImageTintColor:[UIColor WhiteColor]];

共有3个答案

裴翰学
2023-03-14

将@Sven Tiffe的答案扩展到iOS7,您可以让您的代码自动为故事板中添加的未选择的UITabBar图像着色。以下方法将节省您创建两组图标图像(即选择的和未选择的)并以编程方式加载它们的时间。将类别方法Imagewith Color:(请参阅-如何在iOS和WatchKit中更改图像tintColor)添加到您的项目中,然后将以下内容放入您自定义的UITabBarController viewddLoad方法中:

// 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叠加和UIImage叠加. m(从这个答案中提取):

@implementation UIImage(Overlay)

- (UIImage *)imageWithColor:(UIColor *)color1
{
        UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale);
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextTranslateCTM(context, 0, self.size.height);
        CGContextScaleCTM(context, 1.0, -1.0);
        CGContextSetBlendMode(context, kCGBlendModeNormal);
        CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
        CGContextClipToMask(context, rect, self.CGImage);
        [color1 setFill];
        CGContextFillRect(context, rect);
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return newImage;
}
@end
夏侯旻
2023-03-14

据我所知,这在iOS7下是行不通的。特别是,选项卡栏的tintColor将定义选定选项卡的颜色,而不是未选定选项卡的颜色。如果你想在iOS7中更改默认值,似乎你必须实际使用不同的图标(以你喜欢的未选定选项卡的颜色)并设置文本的颜色。

此示例应将选定的选项卡着色为红色,并将其他选项卡呈现为绿色。在您的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下的颜色更简单的方法?

堵景天
2023-03-14

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

A.代码实例(Swift):

self.tabBar.unselectedItemTintColor = unselectedcolor

B. IB实例:

添加类型为:Color

C.全球形象(Swift):

UITabBar.appearance().unselectedItemTintColor = unselectedcolor
 类似资料:
  • 关于选择颜色 可以通过使用 Illustrator 中的各种工具、面板和对话框为图稿选择颜色。如何选择颜色取决于图稿的要求。例如,如果希望使用公司认可的特定颜色,则可以从公司认可的色板库中选择颜色。如果希望颜色与其他图稿中的颜色匹配,则可以使用吸管或拾色器并输入准确的颜色值。 可以使用下列任一功能来选择颜色: 色板面板和色板库面板提供不同的颜色和颜色组。可以从现有的色板和库中选择颜色,也可以创建自

  • 问题内容: 在浏览器中选择文本时,大多数情况下选中的文本后面的背景会变为蓝色。如何将这种颜色更改为另一种颜色? 问题答案: 您正在寻找伪元素。 另外,作为旁注。如果你计划在所有使用文本的影子在你的网站,我建议将你的造型。正如你可以看到在这个小提琴,它的眼睛真的很辛苦。

  • 颜色字段存储一个字符串作为其值,并存储一个字符串作为其文本。 它的值是格式为#rrggbb的字符串,而其文本也可以是格式为#rgb的字符串。 颜色字段 打开编辑器的颜色字段 压缩的颜色字段 新建 { "type": "example_colour", "message0": "colour: %1", "args0": [ { "type": "field_colo

  • 用于颜色选择,支持多种格式。 基础用法 使用 v-model 与 Vue 实例中的一个变量进行双向绑定,绑定的变量需要是字符串类型。 <div class="block"> <span class="demonstration">有默认值</span> <el-color-picker v-model="color1"></el-color-picker> </div> <div clas

  • ColorPicker 颜色选择器 用于颜色选择,支持多种格式。 基础用法 :::demo 通过value属性控制当前显示的颜色。 render() { const color1 = '#20a0ff'; const color2 = null; return ( <div> <div className="block"> <span classNa

  • ColorPicker 颜色选择器 用于颜色选择,支持多种格式。 基础用法 使用 v-model 与 Vue 实例中的一个变量进行双向绑定,绑定的变量需要是字符串类型。 <div class="block"> <span class="demonstration">有默认值</span> <el-color-picker v-model="color1"></el-color-picker