iOS 13 UISegmentedControl适配

1、在iOS13中 ,UISegmentedControl默认样式变为白底黑字,

iOS 13以上
iOS 13以下

实现延展 ,调用 segmentedIOS13Style方法

1、.h文件

@interface UISegmentedControl (Category)

- (void)segmentedIOS13Style;

@end

2、.m文件

#import "UISegmentedControl+Category.h"

@implementation UISegmentedControl (Category)

- (void)segmentedIOS13Style {

    if (@available(iOS 13, *)) {

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

        self.selectedSegmentTintColor = tintColor;

    }

}

- (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();

    return theImage;

}

@end

你可能感兴趣的:(iOS 13 UISegmentedControl适配)