iOS UISwitch 圆点中间添加文字

2020-12-22  本文已影响0人  里克尔梅西
- (UISwitch *)barrageSwitch {
    if (!_barrageSwitch) {
        _barrageSwitch = [[UISwitch alloc] init];
        _barrageSwitch.onTintColor = BB_LoginTheme_Color;
        [_barrageSwitch setOn:YES];
        [_barrageSwitch addTarget:self action:@selector(switchClick:) forControlEvents:UIControlEventValueChanged];
        //添加“弹”字
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
             NSArray * sub1 = self.barrageSwitch.subviews;
                    UIImageView * onImg = nil;
                    for (UIView * v1 in sub1) {
                        NSArray * sub2 = v1.subviews;
                        //iOS12在这里
                        for (UIView * v2 in sub2) {
                            if ([v2 isKindOfClass:UIImageView.class] && v2.width == 43 && v2.height == 43) {
                                onImg = (UIImageView *)v2;
                                break;
                            }
                            //iOS13在这里
                            NSArray * sub3 = v2.subviews;
                            for (UIView * v3 in sub3) {
                                if ([v3 isKindOfClass:UIImageView.class] && v3.width == 43 && v3.height == 43) {
                                    onImg = (UIImageView *)v3;
                                    break;
                                }
                            }
                        }
                    }
                    if (onImg) {
                        UILabel * danmu = [[UILabel alloc] initWithFrame:CGRectMake(8, 5, 27, 27)];
                        danmu.backgroundColor = [UIColor whiteColor];
                        danmu.layer.cornerRadius = danmu.height * 0.5;
                        danmu.layer.masksToBounds = YES;
                        danmu.textColor = BB_LoginTheme_Color;
                        danmu.textAlignment = NSTextAlignmentCenter;
                        danmu.font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
                        danmu.text = @"弹";
                        [onImg addSubview:danmu];
                        self.tanLabel = danmu;
                    }
                });
    }
    return _barrageSwitch;
}

参考链接:https://www.jianshu.com/p/8ad8f695db37

上一篇下一篇

猜你喜欢

热点阅读