public static void UpdateBadge(this UIBarButtonItem barButtonItem, string text, UIColor backgroundColor, UIColor textColor) { var bLayer = GetBadgeLayer(barButtonItem); if (string.IsNullOrEmpty(text) || text == "0") { bLayer?.RemoveFromSuperLayer(); objc_setAssociatedObject(barButtonItem.Handle, BadgeKey.Handle, new CAShapeLayer().Handle, AssociationPolicy.ASSIGN); return; } var textLayer = bLayer?.Sublayers?.First(p => p is CATextLayer) as CATextLayer; if (textLayer != null) { textLayer.String = text; } else { barButtonItem.AddBadge(text, backgroundColor, textColor); } }
public static void UpdateBadge(this UIBarButtonItem barButtonItem, string text, UIColor backgroundColor, UIColor textColor) { var bLayer = GetBadgeLayer(barButtonItem); if (bLayer != null) { bLayer.Hidden = string.IsNullOrEmpty(text) || text == "0"; if (bLayer.Hidden) { return; } } var textLayer = bLayer?.Sublayers?.First(p => p is CATextLayer) as CATextLayer; if (textLayer != null && textLayer.String != "0") { textLayer.String = text; } else { barButtonItem.AddBadge(text, backgroundColor, textColor); } }