效果圖:
以 UIButton 為例:

屏幕快照 2018-09-04 下午5.15.42 2.png
原理
給
UIView寫了一個分類UIView+JHRedDot在設(shè)置小紅點時,監(jiān)聽
view的frame和bounds,以便在view尺寸變化時更新小紅點位置重寫系統(tǒng)函數(shù)
- (void)willMoveToSuperview:(UIView *)newSuperview;來設(shè)置小紅點的初始位置針對
UIButton做了進(jìn)一步處理,自動靠右。
示例
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;