|
|
@@ -12,118 +12,143 @@
|
|
|
|
|
|
@interface UASTabbar()
|
|
|
|
|
|
-@property(strong,nonatomic)UASButton *badgeBtn;
|
|
|
-
|
|
|
+@property(strong,nonatomic)NSMutableArray<UILabel *> *labArr;
|
|
|
+
|
|
|
+@property(assign,atomic)BOOL isExist;
|
|
|
|
|
|
@end
|
|
|
|
|
|
@implementation UASTabbar
|
|
|
|
|
|
--(void)setRemindCount:(NSInteger)remindCount{
|
|
|
+
|
|
|
+-(void)layoutSubviews{
|
|
|
|
|
|
+ [super layoutSubviews];
|
|
|
|
|
|
- if (remindCount>0) {
|
|
|
-
|
|
|
- self.badgeBtn.hidden = NO;
|
|
|
- if (remindCount > 99) {
|
|
|
+ [self addRemindView];
|
|
|
|
|
|
- self.badgeBtn.titleLabel.font = [UIFont systemFontOfSize:8]; //消息大小不变
|
|
|
-
|
|
|
- [self.badgeBtn setTitle:@"99+" forState:UIControlStateNormal];
|
|
|
-
|
|
|
+}
|
|
|
|
|
|
- }else{
|
|
|
-
|
|
|
- self.badgeBtn.titleLabel.font = [UIFont systemFontOfSize:8]; //消息大小不变
|
|
|
+-(void)addRemindView{
|
|
|
+
|
|
|
+ if (self.isExist || self.labArr.count>0) return;
|
|
|
+ self.isExist = YES;
|
|
|
+
|
|
|
+ NSInteger index = 0;
|
|
|
+
|
|
|
+ for (UIView *view in self.subviews) {
|
|
|
+ if ([view isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
|
|
|
|
|
|
- [self.badgeBtn setTitle:[NSString stringWithFormat:@"%ld",(long)remindCount] forState:UIControlStateNormal];
|
|
|
+ for (UIView *viewOne in view.subviews) {
|
|
|
+
|
|
|
+ if ([viewOne isKindOfClass:NSClassFromString(@"UITabBarSwappableImageView")]) {
|
|
|
+
|
|
|
+ UILabel *label = [UILabel new];
|
|
|
+ label.backgroundColor = [UIColor redColor];
|
|
|
+ if (index == 0) {
|
|
|
+
|
|
|
+ label.layer.cornerRadius = 8;
|
|
|
+ label.layer.masksToBounds = YES;
|
|
|
+
|
|
|
+ label.textColor = [UIColor whiteColor];
|
|
|
+
|
|
|
+ label.font = [UIFont systemFontOfSize:8];
|
|
|
+
|
|
|
+ label.textAlignment = NSTextAlignmentCenter;
|
|
|
+
|
|
|
+ [viewOne addSubview:label];
|
|
|
+
|
|
|
+ [label mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
|
+
|
|
|
+ make.top.equalTo(viewOne).offset(-6);
|
|
|
+ make.right.equalTo(viewOne).offset(10);
|
|
|
+ make.width.height.mas_equalTo(16);
|
|
|
+
|
|
|
+ }];
|
|
|
+
|
|
|
+ }else{
|
|
|
+
|
|
|
+ label.layer.cornerRadius = 5;
|
|
|
+ label.layer.masksToBounds = YES;
|
|
|
+
|
|
|
+ [viewOne addSubview:label];
|
|
|
+
|
|
|
+ [label mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
|
+
|
|
|
+ make.top.equalTo(viewOne).offset(-5);
|
|
|
+ make.right.equalTo(viewOne).offset(5);
|
|
|
+ make.width.height.mas_equalTo(10);
|
|
|
+
|
|
|
+ }];
|
|
|
+
|
|
|
+ }
|
|
|
+ index++;
|
|
|
+ label.hidden = YES;
|
|
|
+ [self.labArr addObject:label];
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
- }else{
|
|
|
-
|
|
|
- self.badgeBtn.hidden = YES;
|
|
|
-
|
|
|
}
|
|
|
+
|
|
|
+}
|
|
|
|
|
|
+- (void)showBadgeOnItemIndex:(int)index{
|
|
|
+
|
|
|
+ if (self.labArr.count == 0 || index >= self.items.count) return;
|
|
|
+
|
|
|
+ self.labArr[index].hidden = NO;
|
|
|
+
|
|
|
}
|
|
|
|
|
|
+- (void)hideBadgeOnItemIndex:(int)index{
|
|
|
+
|
|
|
+ if (self.labArr.count == 0 || index >= self.items.count) return;
|
|
|
|
|
|
+ self.labArr[index].hidden = YES;
|
|
|
+
|
|
|
+}
|
|
|
|
|
|
-- (void)showBadgeOnItemIndex:(int)index{
|
|
|
|
|
|
+-(void)setRemindCount:(NSInteger)remindCount{
|
|
|
|
|
|
- if (index > 0) {
|
|
|
- [self removeBadgeOnItemIndex:index];
|
|
|
-
|
|
|
- UIView *badgeView = [[UIView alloc]init];
|
|
|
- badgeView.tag = 9999 + index;
|
|
|
- badgeView.layer.cornerRadius = 5;
|
|
|
- badgeView.backgroundColor = [UIColor redColor];
|
|
|
- CGRect tabFrame = self.frame;
|
|
|
-
|
|
|
- float percentX = (index +0.6) / TabbarItemNums;
|
|
|
- CGFloat x = ceilf(percentX * tabFrame.size.width);
|
|
|
- CGFloat y = ceilf(0.1 * tabFrame.size.height);
|
|
|
- badgeView.frame = CGRectMake(x, y, 10, 10);
|
|
|
- [self addSubview:badgeView];
|
|
|
-
|
|
|
- }else{
|
|
|
-
|
|
|
- CGFloat bageWidth = 16;
|
|
|
- UASButton *badgeBtn = [UASButton new];
|
|
|
- badgeBtn.layer.cornerRadius = bageWidth/2;
|
|
|
- badgeBtn.backgroundColor = [UIColor redColor];
|
|
|
- CGRect tabFrame = self.frame;
|
|
|
+ _remindCount = remindCount;
|
|
|
+
|
|
|
+ if (remindCount > 0) {
|
|
|
|
|
|
- float percent = 0;
|
|
|
+ self.labArr[0].hidden = NO;
|
|
|
|
|
|
- if (SCREEN_WIDTH > 320) {
|
|
|
+ if (remindCount > 99) {
|
|
|
|
|
|
- percent = 0.595;
|
|
|
+ self.labArr[0].font = [UIFont systemFontOfSize:7];
|
|
|
+ self.labArr[0].text = @"99+";
|
|
|
|
|
|
}else{
|
|
|
|
|
|
- percent = 0.625;
|
|
|
-
|
|
|
+ self.labArr[0].font = [UIFont systemFontOfSize:8];
|
|
|
+ self.labArr[0].text = [NSString stringWithFormat:@"%ld",(long)remindCount];
|
|
|
}
|
|
|
|
|
|
- float percentX = (index + percent) / TabbarItemNums;
|
|
|
- CGFloat x = ceilf(percentX * tabFrame.size.width);
|
|
|
- CGFloat y = ceilf(0.08 * tabFrame.size.height);
|
|
|
- badgeBtn.frame = CGRectMake(x, y,bageWidth, bageWidth);
|
|
|
- [self bringSubviewToFront:badgeBtn];
|
|
|
- self.badgeBtn.titleLabel.numberOfLines = 1;
|
|
|
- self.badgeBtn = badgeBtn;
|
|
|
- self.badgeBtn.titleLabel.font = FONT_SIZE(8);
|
|
|
- self.badgeBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
|
|
|
- self.badgeBtn.hidden = YES;
|
|
|
- [self addSubview:self.badgeBtn];
|
|
|
-
|
|
|
+ }else{
|
|
|
+
|
|
|
+ self.labArr[0].hidden = YES;
|
|
|
+
|
|
|
}
|
|
|
-}
|
|
|
-
|
|
|
-- (void)hideBadgeOnItemIndex:(int)index{
|
|
|
|
|
|
- [self removeBadgeOnItemIndex:index];
|
|
|
-}
|
|
|
-
|
|
|
-- (void)removeBadgeOnItemIndex:(int)index{
|
|
|
|
|
|
- for (UIView *subView in self.subviews) {
|
|
|
- if (subView.tag == 9999+index) {
|
|
|
- [subView removeFromSuperview];
|
|
|
- }
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
--(void)layoutSubviews{
|
|
|
-
|
|
|
- [super layoutSubviews];
|
|
|
+-(NSMutableArray *)labArr{
|
|
|
|
|
|
+ if (!_labArr) {
|
|
|
+
|
|
|
+ _labArr = [NSMutableArray new];
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
+ return _labArr;
|
|
|
}
|
|
|
-
|
|
|
@end
|