我想更改背景颜色UITabBarItem
徽章但找不到任何资源如何制作它。
是否有可能更改UITabBarItem徽章颜色
回答
看起来没有。您只能设置该值。 从苹果公司的文档徽章是:与一个 周围红色椭圆形显示于该项目的右上角
文本。
是的,但唯一可能的解决方案是创建一个自定义Tabbar并创建您的自定义标签栏徽章图标。你会发现很多文章/代码来创建自定义tabbar。
我写了这段代码我的应用程序,但我只在iOS的7测试它
for (UIView* tabBarButton in self.tabBar.subviews) {
for (UIView* badgeView in tabBarButton.subviews) {
NSString* className = NSStringFromClass([badgeView class]);
// looking for _UIBadgeView
if ([className rangeOfString:@"BadgeView"].location != NSNotFound) {
for (UIView* badgeSubview in badgeView.subviews) {
NSString* className = NSStringFromClass([badgeSubview class]);
// looking for _UIBadgeBackground
if ([className rangeOfString:@"BadgeBackground"].location != NSNotFound) {
@try {
[badgeSubview setValue:[UIImage imageNamed:@"YourCustomImage.png"] forKey:@"image"];
}
@catch (NSException *exception) {}
}
if ([badgeSubview isKindOfClass:[UILabel class]]) {
((UILabel *)badgeSubview).textColor = [UIColor greenColor];
}
}
}
}
}
你才能够更新徽章背景图像,并不出彩。如果您想以某种方式更新徽章标签,我也会公开徽章标签。
重要的是要注意,必须在设置tabBarItem.badgeValue
之后调用此代码!
编辑:14年4月14日
上面的代码将在iOS中7在任何地方工作时调用。为了让它在iOS 7.1中工作,请在视图控制器-viewWillLayoutSubviews
中调用它。
编辑:14年12月22日
下面是其中我目前使用更新的片段。为简单起见,我将代码放在了类别扩展中。
- (void)badgeViews:(void (^)(UIView* badgeView, UILabel* badgeLabel, UIView* badgeBackground))block {
if (block) {
for (UIView* tabBarButton in self.subviews) {
for (UIView* badgeView in tabBarButton.subviews) {
NSString* className = NSStringFromClass([badgeView class]);
if ([className rangeOfString:@"BadgeView"].location != NSNotFound) {
UILabel* badgeLabel;
UIView* badgeBackground;
for (UIView* badgeSubview in badgeView.subviews) {
NSString* className = NSStringFromClass([badgeSubview class]);
if ([badgeSubview isKindOfClass:[UILabel class]]) {
badgeLabel = (UILabel *)badgeSubview;
} else if ([className rangeOfString:@"BadgeBackground"].location != NSNotFound) {
badgeBackground = badgeSubview;
}
}
block(badgeView, badgeLabel, badgeBackground);
}
}
}
}
}
然后,当你准备好调用它时,它会看起来像这样。
[self.tabBar badgeViews:^(UIView *badgeView, UILabel *badgeLabel, UIView *badgeBackground) {
}];
编辑:15年11月16日
它已经引起了我的注意,有些人需要对所发生的事情在这个代码更清晰。 for循环正在搜索几个不公开的视图。通过检查视图类名是否包含预期名称的一部分,它确保达到预期视图,同时不会引发Apple发出任何可能的红旗。一旦找到了所有东西,就可以轻松访问这些视图来执行一个块。
值得注意的是,该代码可能会在未来的iOS更新中停止工作。例如,这些内部视图有一天会获得不同的类名。然而,这种机会几乎没有,因为即使内部苹果也很少重构类。但即使他们这样做,它也会是UITabBarBadgeView
的标题,它仍然会达到代码中的预期点。由于iOS9已经完成了,而且这些代码仍然按照预期工作,所以您可以预料到这个问题永远不会发生。
我有同样的问题,并通过创建一个小的类别来代替BadgeView与你可以轻松定制的UILabel来解决它。
不,你不能改变颜色,但你可以使用自己的徽章代替。在文件范围内添加此扩展名,然后您可以自定义标签。请在任何根视图控制器中拨打self.tabBarController!.setBadges([1,0,2])
。
要明确这是一个带有三个项目的标签栏,徽章值从左到右。
extension UITabBarController {
func setBadges(badgeValues:[Int]){
var labelExistsForIndex = [Bool]()
for value in badgeValues {
labelExistsForIndex.append(false)
}
for view in self.tabBar.subviews {
if view.isKindOfClass(PGTabBadge) {
let badgeView = view as! PGTabBadge
let index = badgeView.tag
if badgeValues[index]==0 {
badgeView.removeFromSuperview()
}
labelExistsForIndex[index]=true
badgeView.text = String(badgeValues[index])
}
}
for var i=0;i<labelExistsForIndex.count;i++ {
if labelExistsForIndex[i] == false {
if badgeValues[i] > 0 {
addBadge(i, value: badgeValues[i], color:UIColor(red: 4/255, green: 110/255, blue: 188/255, alpha: 1), font: UIFont(name: "Helvetica-Light", size: 11)!)
}
}
}
}
func addBadge(index:Int,value:Int, color:UIColor, font:UIFont){
let itemPosition = CGFloat(index+1)
let itemWidth:CGFloat = tabBar.frame.width/CGFloat(tabBar.items!.count)
let bgColor = color
let xOffset:CGFloat = 12
let yOffset:CGFloat = -9
var badgeView = PGTabBadge()
badgeView.frame.size=CGSizeMake(17, 17)
badgeView.center=CGPointMake((itemWidth * itemPosition)-(itemWidth/2)+xOffset, 20+yOffset)
badgeView.layer.cornerRadius=badgeView.bounds.width/2
badgeView.clipsToBounds=true
badgeView.textColor=UIColor.whiteColor()
badgeView.textAlignment = .Center
badgeView.font = font
badgeView.text = String(value)
badgeView.backgroundColor = bgColor
badgeView.tag=index
tabBar.addSubview(badgeView)
}
}
class PGTabBadge: UILabel {
}
对于使用雨燕的人,我管理,将有工作的任何屏幕大小和任何方向徽章以提高TimWhiting答案。
extension UITabBarController {
func setBadges(badgeValues: [Int]) {
for view in self.tabBar.subviews {
if view is CustomTabBadge {
view.removeFromSuperview()
}
}
for index in 0...badgeValues.count-1 {
if badgeValues[index] != 0 {
addBadge(index, value: badgeValues[index], color:UIColor(paletteItem: .Accent), font: UIFont(name: Constants.ThemeApp.regularFontName, size: 11)!)
}
}
}
func addBadge(index: Int, value: Int, color: UIColor, font: UIFont) {
let badgeView = CustomTabBadge()
badgeView.clipsToBounds = true
badgeView.textColor = UIColor.whiteColor()
badgeView.textAlignment = .Center
badgeView.font = font
badgeView.text = String(value)
badgeView.backgroundColor = color
badgeView.tag = index
tabBar.addSubview(badgeView)
self.positionBadges()
}
override public func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
self.positionBadges()
}
// Positioning
func positionBadges() {
var tabbarButtons = self.tabBar.subviews.filter { (view: UIView) -> Bool in
return view.userInteractionEnabled // only UITabBarButton are userInteractionEnabled
}
tabbarButtons = tabbarButtons.sort({ $0.frame.origin.x < $1.frame.origin.x })
for view in self.tabBar.subviews {
if view is CustomTabBadge {
let badgeView = view as! CustomTabBadge
self.positionBadge(badgeView, items:tabbarButtons, index: badgeView.tag)
}
}
}
func positionBadge(badgeView: UIView, items: [UIView], index: Int) {
let itemView = items[index]
let center = itemView.center
let xOffset: CGFloat = 12
let yOffset: CGFloat = -14
badgeView.frame.size = CGSizeMake(17, 17)
badgeView.center = CGPointMake(center.x + xOffset, center.y + yOffset)
badgeView.layer.cornerRadius = badgeView.bounds.width/2
tabBar.bringSubviewToFront(badgeView)
}
}
class CustomTabBadge: UILabel {}
改变徽章的颜色现在原生支持iOS中10及更高版本使用UITab
内badgeColor
财产。请参阅apple docs以了解酒店的更多信息。
实施例:
- 夫特3:
myTab.badgeColor = UIColor.blue
- 目的-C:
[myTab setBadgeColor:[UIColor blueColor]];
如果([MYTAB respondsToSelector? :@选择(setBadgeColor :)]){ [MYTAB setBadgeColor:[的UIColor blueColor]];} 你 – rjobidon
能解释一下这是如何实际执行?我有点困惑。 –
// change TabBar BadgeView background Color
-(void)changeTabBarBadgeViewBgColor:(UITabBar*)tabBar {
for (UIView* tabBarButton in tabBar.subviews) {
for (UIView* badgeView in tabBarButton.subviews) {
NSString* className = NSStringFromClass([badgeView class]);
// looking for _UIBadgeView
if ([className rangeOfString:@"BadgeView"].location != NSNotFound) {
for (UIView* badgeSubview in badgeView.subviews) {
NSString* className = NSStringFromClass([badgeSubview class]);
// looking for _UIBadgeBackground
if ([className rangeOfString:@"BadgeBackground"].location != NSNotFound) {
@try {
[badgeSubview setValue:nil forKey:@"image"];
[badgeSubview setBackgroundColor:[UIColor blueColor]];
badgeSubview.clipsToBounds = YES;
badgeSubview.layer.cornerRadius = badgeSubview.frame.size.height/2;
}
@catch (NSException *exception) {}
}
if ([badgeSubview isKindOfClass:[UILabel class]]) {
((UILabel *)badgeSubview).textColor = [UIColor greenColor];
}
}
}
}
}
}
UITabBarItem
现在有在IOS 10
var badgeColor: UIColor? { get set }
它也可通过出现。
if #available(iOS 10, *) {
UITabBarItem.appearance().badgeColor = .green
}
参考文档: https://developer.apple.com/reference/uikit/uitabbaritem/1648567-badgecolor
当我还想支持iOS9时会发生什么?我是否需要将其放在'respondsToSelector'或'#available'调用中? – Jens
不是那样的。 'if(@available(iOS 10,*))'... –
看看这里@UITabbarItem-CustomBadge。
的完整演示是继
它只有两个行代码,如果你想使用的默认实现
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//supplying the animation parameter
[UITabBarItem setDefaultAnimationProvider:[[DefaultTabbarBadgeAnimation alloc] init]];
[UITabBarItem setDefaultConfigurationProvider:[[DefaultSystemLikeBadgeConfiguration alloc] init]];
//rest of your code goes following...
return YES;
}
斯威夫特3这里是一个更新版本@Kirualex的回答(谁提高了@TimWhiting的答案)Swift 3.
extension UITabBarController {
func setBadges(badgeValues: [Int]) {
for view in self.tabBar.subviews {
if view is CustomTabBadge {
view.removeFromSuperview()
}
}
for index in 0...badgeValues.count-1 {
if badgeValues[index] != 0 {
addBadge(index: index, value: badgeValues[index], color: UIColor.blue, font: UIFont(name: "Helvetica-Light", size: 11)!)
}
}
}
func addBadge(index: Int, value: Int, color: UIColor, font: UIFont) {
let badgeView = CustomTabBadge()
badgeView.clipsToBounds = true
badgeView.textColor = UIColor.white
badgeView.textAlignment = .center
badgeView.font = font
badgeView.text = String(value)
badgeView.backgroundColor = color
badgeView.tag = index
tabBar.addSubview(badgeView)
self.positionBadges()
}
override open func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
self.positionBadges()
}
// Positioning
func positionBadges() {
var tabbarButtons = self.tabBar.subviews.filter { (view: UIView) -> Bool in
return view.isUserInteractionEnabled // only UITabBarButton are userInteractionEnabled
}
tabbarButtons = tabbarButtons.sorted(by: { $0.frame.origin.x < $1.frame.origin.x })
for view in self.tabBar.subviews {
if view is CustomTabBadge {
let badgeView = view as! CustomTabBadge
self.positionBadge(badgeView: badgeView, items:tabbarButtons, index: badgeView.tag)
}
}
}
func positionBadge(badgeView: UIView, items: [UIView], index: Int) {
let itemView = items[index]
let center = itemView.center
let xOffset: CGFloat = 12
let yOffset: CGFloat = -14
badgeView.frame.size = CGSize(width: 17, height: 17)
badgeView.center = CGPoint(x: center.x + xOffset, y: center.y + yOffset)
badgeView.layer.cornerRadius = badgeView.bounds.width/2
tabBar.bringSubview(toFront: badgeView)
}
}
class CustomTabBadge: UILabel {}
- 1. 是否可以自定义UITabBarItem徽章?
- 2. 如何更改UITabBarItem徽章位置?
- 3. 调整UITabBarItem徽章位置?
- 4. 更改Swift中UITabBarItem的背景颜色
- 5. 更改UITabBarItem图像的颜色
- 6. 是否有可能更改ControlsFX中的ToggleSwitch颜色
- 7. 是否有可能在android中更改ActionBar颜色?
- 8. 是否有可能在android中更改所选Tab的颜色?
- 9. 是否有可能在Android上更改通知点颜色Oreo
- 10. 是否有可能更改struts2-jquery-grid-tags的css颜色
- 11. 如何更改UIBarbutton中的徽章背景颜色?
- 12. 在zabuto日历中更改徽章的颜色
- 13. Monotouch:BarItem徽章色调的颜色?
- 14. 是否可以更改ScrollBar的颜色?
- 15. 是否可以更改Apple Map颜色?
- 16. swift UITabbaritem颜色
- 17. Rails:是否有可能获得优异宝石临时徽章
- 18. UITabBarItem的快速设置徽章值
- 19. iPhone:从uitabbaritem读取徽章值问题
- 20. Chrome扩展徽章 - 文字颜色
- 21. 更改只有一个UITabBarItem的背景颜色
- 22. 是否有可能改变一个字符串颜色JOptionPane.showMessageDialog()
- 23. 是否有可能改变按钮的颜色?
- 24. 更改UITabBarItem未选中的颜色色调 - Swift
- 25. 在iOS 9中更改UITabBarItem的色调颜色?
- 26. 如何在uitabbarcontroller中更改默认的uitabbaritem的灰色颜色?
- 27. 在iOS 10中更改UITabBarItem的选定色调颜色
- 28. 在firefox中,使用crossrider API(setBadgeText),徽章颜色不会改变?
- 29. 如何将IOS徽章通知颜色从默认红色更改为其他颜色?
- 30. 更改文章的背景颜色
提供的解决方案适用于iOS 7,但不适用于iOS7.1。任何事情都可以做到7.1? – CrazyDeveloper
确保在您的视图控制器中调用了“-viewWillLayoutSubviews”。 – cnotethegr8
@jeffamaphone OP问,'有可能'。他们从未要求实现不会中断。你给我弃权的理由是不够的。我请你好好扭转行动。 – cnotethegr8