效果图:
以 UIButton 为例:

屏幕快照 2018-09-04 下午5.15.42 2.png
原理
给
UIView写了一个分类UIView+JHRedDot在设置小红点时,监听
view的frame和bounds,以便在view尺寸变化时更新小红点位置重写系统函数
- (void)willMoveToSuperview:(UIView *)newSuperview;来设置小红点的初始位置针对
UIButton做了进一步处理,自动靠右。
示例
button.frame = CGRectMake(0, 0, 34, 44);
button.jh_redDot = [JHRedDot redDotWithConfig:({
JHRedDotConfig *config = [[JHRedDotConfig alloc] init];
config.offsetY = 5;
config;
})];
button.jh_redDot.hidden = YES;