创建顶部带有徽章的 BarButtonItem。易于自定义。您的 BarButtonItem 可以是任何您想要的自定义视图。顶部的徽章可以显示任何数字或字符串,大小或长度不限。
相当简单,只需下载并将 "BBBadgeBarButtonItem.h" 和 "BBBadgeBarButtonItem.m" 添加到您的 xcodeproject 中。别忘了在每个需要的地方导入头文件
#import "BBBadgeBarButtonItem.h"
然后,您只需实例化美丽的 BBBadgeBarButtonItem 并将其添加到您的导航栏
UIButton *customButton = [[UIButton alloc] init];
//...
// Create and add our custom BBBadgeBarButtonItem
BBBadgeBarButtonItem *barButton = [[BBBadgeBarButtonItem alloc] initWithCustomUIButton:customButton];
// Set a value for the badge
barButton.badgeValue = @"1";
// Add it as the leftBarButtonItem of the navigation bar
self.navigationItem.leftBarButtonItem = barButton;
如果您希望 BarButtonItem 处理触摸事件和点击,请使用 UIButton 作为 customView。BBButtonItem 显示的图标或文本是您的自定义视图。
查看 BBBadgeBarButtonItem.h 以了解如何轻松快速地自定义徽章。请记住,每次改变这些值之一时,徽章都会直接刷新以处理您的样式偏好。
// Each time you change one of properties, the badge will refresh with your changes
// Badge value to be display
@property (nonatomic) NSString *badgeValue;
// Badge background color
@property (nonatomic) UIColor *badgeBGColor;
// Badge text color
@property (nonatomic) UIColor *badgeTextColor;
// Badge font
@property (nonatomic) UIFont *badgeFont;
// Padding value for the badge
@property (nonatomic) CGFloat badgePadding;
// Minimum size badge to small
@property (nonatomic) CGFloat badgeMinSize;
// Values for offseting the badge over the BarButtonItem you picked
@property (nonatomic) CGFloat badgeOriginX;
@property (nonatomic) CGFloat badgeOriginY;
// In case of numbers, remove the badge when reaching zero
@property BOOL shouldHideBadgeAtZero;
// Badge has a bounce animation when value changes
@property BOOL shouldAnimateBadge;
您还可以选择关闭在更改徽章值时触发的微小的弹跳动画,或者决定是否显示 0。
该类与 iOS >= 6.0 兼容。
还有一个小演示项目供您参考 ;)
任何建议都受欢迎!因为我正试图学习最佳实践,更好地理解行为和 Objective-C!感谢。