美文网首页
iOS 13 UISegmentedControl 适配

iOS 13 UISegmentedControl 适配

作者: 火星村村长 | 来源:发表于2019-12-17 09:33 被阅读0次

    写个UISegmentedControl 的分类,调用ensureiOS12Style

    @interface UISegmentedControl(Common)

    -(void)ensureiOS12Style;

    @end

    @implementation UISegmentedControl(Common)

    -(void)ensureiOS12Style{

    // UISegmentedControl has changed in iOS 13 and setting the tint color now has no effect.

    if(@available(iOS13,*)){

    [self setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor], NSFontAttributeName: [UIFont boldSystemFontOfSize:14.0f]} forState:UIControlStateSelected];

    UIColor*tintColor=[self tintColor];UIImage*tintColorImage=[self imageWithColor:tintColor];

    // Must set the background image for normal to something (even clear) else the rest won't work

    [self setBackgroundImage:[self imageWithColor:self.backgroundColor?self.backgroundColor:[UIColor clearColor]]forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

    [self setBackgroundImage:tintColorImage forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];

    [self setBackgroundImage:[self imageWithColor:[tintColor colorWithAlphaComponent:0.2]]forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];

    [self setBackgroundImage:tintColorImage forState:UIControlStateSelected|UIControlStateSelected barMetrics:UIBarMetricsDefault];

    [self setTitleTextAttributes:@{NSForegroundColorAttributeName:tintColor,NSFontAttributeName:[UIFont systemFontOfSize:13]}forState:UIControlStateNormal];

    [self setDividerImage:tintColorImage forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

    self.layer.borderWidth=1;self.layer.borderColor=[tintColor CGColor];}}-(UIImage*)imageWithColor:(UIColor*)color{CGRect rect=CGRectMake(0.0f,0.0f,1.0f,1.0f);

    UIGraphicsBeginImageContext(rect.size);

    CGContextRef context=UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context,[color CGColor]);

    CGContextFillRect(context,rect);

    UIImage*theImage=UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    returntheImage;

    }

    @end

    相关文章

      网友评论

          本文标题:iOS 13 UISegmentedControl 适配

          本文链接:https://www.haomeiwen.com/subject/xyzjnctx.html