Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.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
UITabBar在iOS7上更改一个UITABBARIEM的背景色_Ios_Objective C_Ios7_Uitabbar_Uitabbaritem - Fatal编程技术网

UITabBar在iOS7上更改一个UITABBARIEM的背景色

UITabBar在iOS7上更改一个UITABBARIEM的背景色,ios,objective-c,ios7,uitabbar,uitabbaritem,Ios,Objective C,Ios7,Uitabbar,Uitabbaritem,我可以更改UITabBar中特定的uitabaritem的背景色吗 我知道如何使用以下命令更改选定背景的所有背景: [[UITabBar appearance] setBarTintColor:[UIColor redColor]]; [[UITabBar appearance] setTintColor:[UIColor blueColor]]; [[UITabBar appearance] setSelectedImageTintColor:[UIColor yellowColor]];

我可以更改
UITabBar
中特定的
uitabaritem
的背景色吗

我知道如何使用以下命令更改选定背景的所有背景:

[[UITabBar appearance] setBarTintColor:[UIColor redColor]];
[[UITabBar appearance] setTintColor:[UIColor blueColor]];
[[UITabBar appearance] setSelectedImageTintColor:[UIColor yellowColor]];
但是,如果不进行子类化,是否可以只对一个项执行此操作


谢谢

使用“着色颜色”属性无法真正做到这一点。请了解这样一个事实,即即使是
setSelectedImageTintColor
似乎也没有真正起作用(上次我检查时它没有起作用)


解决方案是动态更改所讨论项目的
tintColor
。只要所选项目发生更改,您就可以通过实现
uitabardelegate
方法
tabBar:didSelectItem:
来临时执行此操作,例如在应用程序委托中。

您可以将子视图添加到父选项卡栏,并在子视图上设置背景色。可以使用选项卡栏框架尺寸计算选项卡项的偏移和宽度,然后在下方插入子视图

示例(Swift):

在Swift中: 此解决方案适用于使用
自动布局的应用程序。其他解决方案的主要区别在于:
tabBar.frame.width
并没有获得实际设备的屏幕宽度。因此,
bgView
出现在错误的位置

您可以使用以下命令进行修复:
UIScreen.main.bounds.width

let tabWidth: CGFloat = UIScreen.main.bounds.width / CGFloat(self.tabbar!.items!.count)
let tabIndex: CGFloat = 2
let bgColor: UIColor = .redColor
let bgView = UIView(frame: CGRectMake(tabWidth * tabIndex, 0, tabWidth, 49))
bgView.backgroundColor = bgColor
self.tabbar!.insertSubview(bgView, atIndex: 0)
49是默认高度
uitabar

目标C解决方案:

int itemIndex = 3;
UIColor* bgColor = [UIColor colorWithRed:(245/255.f) green:(192/255.f) blue:(47/255.f) alpha:1];

float itemWidth = self.tabBarController.tabBar.frame.size.width / 5.0f; //5 = tab bar items
UIView* bgView = [[UIView alloc]initWithFrame: CGRectMake(itemWidth*itemIndex, 0,itemWidth, self.tabBarController.tabBar.frame.size.height)];
bgView.backgroundColor = bgColor;
[self.tabBarController.tabBar insertSubview:bgView atIndex:1];
为Swift 4更新:

let itemIndex: CGFloat = 2.0
let bgColor = UIColor.ownBlue

let itemWidth = tabBar.frame.width / CGFloat(tabBar.items!.count)
let bgView = UIView(frame: CGRect(x: itemWidth * itemIndex, y: 0, width: itemWidth, height: tabBar.frame.height))

bgView.backgroundColor = bgColor
tabBar.insertSubview(bgView, at: 0)

嘿@Mundi,谢谢你的回答。我不想在它被选中时更改它,但一直都是这样。我基本上有五个按钮都有白色背景,除了中间有一个一直有黑色背景。@ HuguesBR,你找到了一个方法来做这个吗?不是真的,我给标签栏添加了一个UBIUTN视图,它屏蔽了这个标签来做这个把戏…谢谢你@ Nahan.f77它工作,唯一的问题是,只允许我改变背景颜色(我接受了你的答案,因为这实际上是我的问题)。但是当我改变背景颜色时,我也需要改变这个项目的色调。。现在,我在tarBar顶部的物品位置添加了一个按钮…:(当我复制并粘贴此代码时,xCode在以下行中给出了一个错误:“找不到成员框架”:let bgView=…@ChrisHarrison-这很奇怪,因为您在上面的行中调用了
tabBar.frame
,因此它也应该在那里给出一个错误。@nathan.f77更改为此行:let itemWidth:CGFloat=tabBar.frame.width/CGF。)loat(tabBar.items!.count)解决了这个问题。这真的很聪明。我以为insertSubview:会阻止图标和文本。结果不是这样。不确定这是否是由最近对自动布局的更改引起的,但这不会起作用。在最后一个代码行的索引0处插入新视图将有其他子视图(最有可能是背景视图)覆盖我们的新视图。与所有视图定义一样,子视图的顺序对渲染很重要。要将新视图置于背景视图之上,但置于tabIndex项目的图像和文本之下,这似乎可以解决iOS 9/Xcode 7.3:self.tabBar!.insertSubview(bgView,atIndex:tabIndex+2)的问题太好了!谢谢!我确认它在iOS 12.1和Xcode 10.1上运行。
let itemIndex: CGFloat = 2.0
let bgColor = UIColor.ownBlue

let itemWidth = tabBar.frame.width / CGFloat(tabBar.items!.count)
let bgView = UIView(frame: CGRect(x: itemWidth * itemIndex, y: 0, width: itemWidth, height: tabBar.frame.height))

bgView.backgroundColor = bgColor
tabBar.insertSubview(bgView, at: 0)