由于UITabBarItem上系統(tǒng)自帶的bage并不能滿足項(xiàng)目的需求往史,所以需要自定義小紅點(diǎn)守呜,以來(lái)展示粱玲。下面為實(shí)現(xiàn)代碼,簡(jiǎn)單方便莽鸿。當(dāng)然如果需要其他樣式昧旨,自己完全可以根據(jù)以下方法進(jìn)行改造。
//
// UITabBar+YGDot.h
// DailyYoga
//
// Created by Beryter on 2017/1/24.
// Copyright ? 2017年 DailyYoga. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UITabBar (YGDot)
/*!
* @brief 顯示小紅點(diǎn)
* @param index 將要顯示小紅點(diǎn)的tabbarItem的索引(第一個(gè)item的索引為0)
* @return
*/
- (void)showDotAtIndex:(NSInteger)index;
/*!
* @brief 隱藏小紅點(diǎn)
* @param index 將要隱藏小紅點(diǎn)的tabbarItem的索引(第一個(gè)item的索引為0)
* @return
*/
- (void)hiddenDotAtIndex:(NSInteger)index;
@end
//
// UITabBar+YGDot.m
// DailyYoga
//
// Created by Beryter on 2017/1/24.
// Copyright ? 2017年 DailyYoga. All rights reserved.
//
#import "UITabBar+YGDot.h"
@implementation UITabBar (YGDot)
- (void)showDotAtIndex:(NSInteger)index
{
NSMutableArray *array = [NSMutableArray array];
for (UIView *view in self.subviews) {
if ([view isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
[array addObject:view];
}
}
if (index >= array.count) {
return;
}
UIView *tabBarButton = array[index];
CGFloat selectedImageWidth = 0;
CGFloat topMargin = 0;
for (UIView *view in tabBarButton.subviews) {
if ([view isKindOfClass:NSClassFromString(@"UITabBarSwappableImageView")]) {
selectedImageWidth = view.frame.size.width;
topMargin = view.frame.origin.y;
}
}
for (UIView *view in tabBarButton.subviews) {
if (view.tag == 999) {
[view removeFromSuperview];
}
}
UIView *dot = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMidX(tabBarButton.bounds) + selectedImageWidth / 2 + 2.5, topMargin, 2.5 * 2, 2.5 * 2)];
dot.backgroundColor = [UIColor redColor];
dot.layer.cornerRadius = 2.5;
dot.tag = 999;
[tabBarButton addSubview:dot];
}
- (void)hiddenDotAtIndex:(NSInteger)index
{
NSMutableArray *array = [NSMutableArray array];
for (UIView *view in self.subviews) {
if ([view isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
[array addObject:view];
}
}
if (index >= array.count) {
return;
}
UIView *tabBarButton = array[index];
for (UIView *view in tabBarButton.subviews) {
if (view.tag == 999) {
[view removeFromSuperview];
}
}
}
@end
可加群一起交流共同學(xué)習(xí):801216530祥得。