美文网首页UIiOS基础类
UITabBarItem添加小红点

UITabBarItem添加小红点

作者: 农村高富帅 | 来源:发表于2017-01-04 18:51 被阅读0次

    引言

    昨天研究过给UIView添加小红点,之后产品提出需求,要给UITabBar的UITabBarItem添加小,由于原生的小红点只是包含数字的那一种,并不能满足需求,于是只能自己来实现。

    具体实现

    1,寻找UITabBarItem上的view

    由于UITabBarItem并不是继承自UIView,所以我认为其UI显示的任务应该是通过该类中某个继承自UIView的属性来实现的,但是苹果文档没找到符合的属性。这时首先想到通过runtime方式获取其所有的属性列表,方法如下

      //返回当前类的所有属性
    - (NSMutableArray *)getProperties:(Class)cls{
    
    // 获取当前类的所有属性
    unsigned int count;// 记录属性个数
    objc_property_t *properties = class_copyPropertyList(cls, &count);
    // 遍历
    NSMutableArray *mArray = [NSMutableArray array];
    for (int i = 0; i < count; i++) {
        
        // An opaque type that represents an Objective-C declared property.
        // objc_property_t 属性类型
        objc_property_t property = properties[i];
        // 获取属性的名称 C语言字符串
        const char *cName = property_getName(property);
        // 转换为Objective C 字符串
        NSString *name = [NSString stringWithCString:cName encoding:NSUTF8StringEncoding];
        [mArray addObject:name];
    }
    
    return mArray.copy;
    }
    

    然后打印获取的列表

    NSLog(@"%@",[self getProperties:[item1 class]]);//item1类型为UITabBarItem
    

    打印如下

    Paste_Image.png

    并么有我想要的结果。蛋疼。。。
    纠结了半天,想到看看UITabBarItem的方法列表,于是网上找方法,如下

      /* 获取对象的所有方法 */
    - (NSMutableArray *)getAllMethods:(Class)cls
    {
    unsigned int mothCout_f =0;
    Method* mothList_f = class_copyMethodList(cls,&mothCout_f);
    
    NSMutableArray *mArray = [NSMutableArray array];
    for(int i=0;i<mothCout_f;i++)
    {
        Method temp_f = mothList_f[i];
        const char* name_s =sel_getName(method_getName(temp_f));
        [mArray addObject:[NSString stringWithUTF8String:name_s]];
    }
    return mArray;
    }
    

    然后,调用,打印,运行,结果如下(太长了,直接复制了)

    ".cxx_destruct",
    "setEnabled:",
    "setTarget:",
    action,
    "setView:",
    "_updateView",
    isSystemItem,
    systemItem,
    "setTitleTextAttributes:forState:",
    "_setTintColor:",
    "_tintColor",
    "setAction:",
    "initWithTitle:image:tag:",
    selectedImage,
    unselectedImage,
    "setBadgeValue:",
    "initWithTabBarSystemItem:tag:",
    "_internalTitle",
    badgeValue,
    "_internalTemplateImage",
    "titleTextAttributesForState:",
    "_barMetrics",
    "_imageStyle",
    "_setBarMetrics:",
    "_setImageStyle:",
    "_showSelectedIndicator:changeSelection:",
    "_updateButtonForTintColor:selected:",
    "_updateImageWithTintColor:isSelected:getImageOffset:",
    imageInsets,
    "setSelectedImage:",
    hasTitle,
    resolvedTitle,
    "_imageForState:metrics:position:type:",
    "_createViewForTabBar:asProxyView:",
    "_setSelected:",
    "_setTitleTextAttributeValue:forAttributeKey:state:",
    "_updateViewAndPositionItems:",
    "setImageInsets:",
    "setBadgeTextAttributes:forState:",
    "badgeTextAttributesForState:",
    badgeColor,
    "setBadgeColor:",
    "_updateToMatchCurrentState",
    "setTitlePositionAdjustment:",
    titlePositionAdjustment,
    "_setInternalTitle:",
    "_setInternalTemplateImage:",
    "_internalTemplateImages",
    "_updateViewBadge",
    "initWithTitle:image:selectedImage:",
    "setFinishedSelectedImage:withFinishedUnselectedImage:",
    finishedSelectedImage,
    finishedUnselectedImage,
    "setAnimatedBadge:",
    animatedBadge,
    "setUnselectedImage:",
    title,
    "setTitle:",
    isEnabled,
    "encodeWithCoder:",
    "initWithCoder:",
    init,
    view,
    image,
    "setImage:",
    "_isSelected"
    

    当我看到“view”和“setView”两方法时,内心是激动的。平静下心情,通过kvc的方式打印其值

    UIView *barButtonView = [item1 valueForKeyPath:@"view"];
    NSLog(@"%@",barButtonView);
    

    结果

    Paste_Image.png

    UITabBarButton是什么鬼,OC中竟然没有暴露这种类型,不看名字应该是UIButotn的子类,于是在barButtonView上添加一个view

    UILabel *label = [[UILabel alloc] initWithFrame:barButtonView.bounds];
    label.backgroundColor = [UIColor redColor];
    [barButtonView addSubview:label];
    [barButtonView bringSubviewToFront:label];
    

    运行,结果

    Paste_Image.png

    从结果来看,这并不是我想要,但是也离成功很近了,此时,我看了看图层,发现barButtonView有三个subView,其中一个是我刚才add上的label,另外两个分别是UITabBarSwappableImageView和UITabBarButtonLabel,这两个用来显示title和image。终于被我找到了。
    打印barButtonView的所有子view

    NSLog(@"%@",barButtonView.subviews);
    

    结果如下

    Paste_Image.png

    OK,至此,需要的view算是找到了。

    2,添加badgeView

    UIView *swappableImageView = [barButtonView.subviews firstObject];
    
    CGFloat badgeWidth = 7;
    UIView *badgeView = [[UIView alloc] initWithFrame:CGRectMake(swappableImageView.frame.size.width - badgeWidth / 2, - badgeWidth / 2, badgeWidth, badgeWidth)];
    badgeView.backgroundColor = [UIColor redColor];
    badgeView.layer.masksToBounds = YES;
    badgeView.layer.cornerRadius = badgeWidth / 2;
    [swappableImageView addSubview:badgeView];
    

    运行,结果如下

    Paste_Image.png

    结束

    • 只能说oc的runtime太强大了,简直无所不能。

    相关文章

      网友评论

        本文标题:UITabBarItem添加小红点

        本文链接:https://www.haomeiwen.com/subject/jlxavttx.html