天天看点

iOS 小红点方案

效果图:

原理

  • 给​

    ​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;      

仓库

  • github:​​https://github.com/xjh093/JHRedDot​​