跳过正文
  1. Posts/

UITabbar自定义Badge

··1 分钟

2018.7.10

tabBarItem的Badge默认样式是带数字的,但是产品要求只要一个小红点,不需要数字,这就需要我们自定义Badge了。

用Reveal分析UITabBar,发现每个按钮是一个UITabBarButton,层级如下:

-UITabBarButton –UITabBarSwappableImageView // 图标 –UITabBarButtonLabel // 文字

如何从UITabBar中找到对应index的UITabBarButton呢?我们断点调试下,可以看到可以直接从UITabBar中用KVC取出。

QQ20180711-174329

实现方案如下:

  • 用KVC找到UITabBarSwappableImageView,关键函数__iconViewWithIndex
  • 新建Badge,加到UITabBarSwappableImageView上
  • 新建Badge的时候设置Tag,通过Tag来移除Badge

上代码

static NSInteger const kBadgeViewTagBase = 10000;

@implementation UITabBar (badge)

// 显示Badge
- (void)showBadgeOnItemIndex:(int)index {
    if (index >= self.items.count) {
        return;
    }
    
    // 如果之前添加过,直接设置hidden为NO
    UIView *icon = [self __iconViewWithIndex:index];
    for (UIView *subView in icon.subviews) {
        if (subView.tag == kBadgeViewTagBase) {
            subView.hidden = NO;
            return;
        }
    }
    
    UIView *badgeView = [[UIView alloc] init];
    badgeView.tag = kBadgeViewTagBase;
    badgeView.layer.cornerRadius = 5;
    badgeView.backgroundColor = [UIColor redColor];
    badgeView.frame = CGRectMake(icon.frame.size.width - 5, 0, 9, 9);
    [icon addSubview:badgeView];
}

// 隐藏Badge
- (void)hideBadgeOnItemIndex:(int)index {
    UIView *icon = [self __iconViewWithIndex:index];
    for (UIView *subView in icon.subviews) {
        if (subView.tag == kBadgeViewTagBase) {
            subView.hidden = YES;
        }
    }
}

// 获取图标所在View
- (UIView *)__iconViewWithIndex:(int)index {
    UITabBarItem *item = self.items[index];
    UIView *tabBarButton = [item valueForKey:@"_view"];
    UIView *icon = [tabBarButton valueForKey:@"_info"];
    return icon;
}

@end

相关文章

计算文字长度

··2 分钟
2018.7.9 官方文档 方法定义 # - (NSRect)boundingRectWithSize:(NSSize)size options:(NSStringDrawingOptions)options attributes:(NSDictionary<NSAttributedStringKey, id> *)attributes context:(NSStringDrawingContext *)context; 参数定义 # size # 绘制的限制size,计算出来的值不会超过这个大小。

UITextField控制输入长度

··1 分钟
2018.7.6 有些时候会有控制输入框文字长度的需求,记录一个简单的思路。 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { if (string.length == 0) { return YES; } NSInteger limit = 15; // 文本的最大长度 NSString *newStr = [textField.text stringByAppendingString:string]; // 修改之后的新字符串 NSInteger newStrLength = newStr.length; newStrLength -= [textField textInRange:[textField markedTextRange]].length; // 去掉高亮内容,输入中文拼音的情况 if (newStrLength > limit) { // 处理composed character, 比如emoji NSString *tempStr = [newStr substringWithRange:[newStr rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, limit)]]; textField.text = tempStr; return NO; } return YES; } 有两个坑注意下:

property or synthsize

··1 分钟
记录时间:2018.7.5 @property (nonatomic, retain) NSObject *var; 生成var的set、get方法的方法声明 生成var的set、get方法的实现(早期版本编译器不生成) 生成成员变量_var(早期版本编译器不生成) @synthsize var = _var

Tagged Pointer小记

··3 分钟
记录时间:2018.7.4 本文使用的测试环境是arm64架构真机 为了探究Tagged Pointer本质,可以查看runtime源码,主要看文件objc-internal.h。

URLWithString return nil

··1 分钟
记录时间:2018.7.3 问题描述 # 在使用URLWithString生成NSURL时,如果出现中文,会导致返回的NSURL为nil。代码如下: