111*_*110 29 iphone objective-c uitabbaritem uitabbar ios
我想更改UITabBarItem
徽章的背景颜色,但找不到任何有关如何制作它的资源.
Nun*_*ves 32
UITabBarItem
从iOS 10开始提供此功能.
var badgeColor: UIColor? { get set }
Run Code Online (Sandbox Code Playgroud)
它也可以通过外观获得.
if #available(iOS 10, *) {
UITabBarItem.appearance().badgeColor = .green
}
Run Code Online (Sandbox Code Playgroud)
参考文档:https: //developer.apple.com/reference/uikit/uitabbaritem/1648567-badgecolor
Han*_*hel 14
iOS 10及更高版本现在支持更改徽章颜色,使用badgeColor
您内部的属性UITabBarItem
.有关该酒店的更多信息,请参阅apple docs.
例:
myTab.badgeColor = UIColor.blue
[myTab setBadgeColor:[UIColor blueColor]];
cno*_*gr8 12
我为我的应用程序编写了这段代码,但我只在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];
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
您只能使用图像更新徽章背景,而不是颜色.如果您想以某种方式更新徽章标签,我也会公开它.
重要的是要注意在设置之后必须调用此代码tabBarItem.badgeValue
!
编辑:14/14/14
以上代码在任何地方调用时都可以在iOS 7中使用.要使其在iOS 7.1中工作,请在视图控制器中调用它-viewWillLayoutSubviews
.
编辑:12/22/14
这是我目前正在使用的更新片段.为简单起见,我将代码放在类别扩展中.
- (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);
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后当你准备好打电话时,它看起来就像这样.
[self.tabBar badgeViews:^(UIView *badgeView, UILabel *badgeLabel, UIView *badgeBackground) {
}];
Run Code Online (Sandbox Code Playgroud)
编辑:11/16/15
引起我的注意的是,有些人需要更清楚地了解此代码中发生的事情.for循环正在搜索一些不可公开访问的视图.通过检查视图类名是否包含预期名称的一部分,它确保到达预期视图,同时不会出现Apple的任何可能的危险信号.找到所有内容后,将执行一个块,可以轻松访问这些视图.
值得注意的是,此代码可能会在未来的iOS更新中停止工作.例如,这些内部视图有一天可能会获得不同的类名.然而,这种可能性几乎没有,因为即使在内部,Apple也很少重构这种性质的课程.但即使他们这样做,它也会成为标题之一UITabBarBadgeView
,它仍然会达到代码中的预期点.由于iOS9完全没有出门,而且这段代码仍在按预期运行,您可以预期这个问题永远不会出现.
小智 10
我有同样的问题并通过创建一个用可以轻松自定义的UILabel替换BadgeView的小类来解决它.
https://github.com/enryold/UITabBarItem-CustomBadge/
Kir*_*lex 10
对于使用Swift的人来说,我设法改进了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 {}
Run Code Online (Sandbox Code Playgroud)
不,你不能改变颜色,但你可以使用自己的徽章.在文件范围添加此扩展,您可以随意自定义徽章.只需调用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 {
}
Run Code Online (Sandbox Code Playgroud)
Swift 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 {}
Run Code Online (Sandbox Code Playgroud)