之前项目中使用UISegmentedControl样式,修改了选中的样式,如图一
图一在iOS13中 ,UISegmentedControl默认样式变为白底黑字,如图二
图二解决方法:
extension UISegmentedControl {
public func ensureiOS12Style() {
if#available(iOS13, *) {
lettintColorImage =UIColor.white.asImage(CGSize.init(width:self.wk_size.width, height:self.wk_size.height))
letDividerColorImage =UIColor.white.asImage(CGSize.init(width:1, height:self.wk_size.height))
setBackgroundImage(UIColor.white.asImage(CGSize.init(width:self.wk_size.width, height:self.wk_size.height)), for: .normal, barMetrics: .default)
setBackgroundImage(tintColorImage, for: .selected, barMetrics: .default)
setBackgroundImage(tintColorImage, for: .highlighted, barMetrics: .default)
setBackgroundImage(tintColorImage, for: [.highlighted, .selected], barMetrics: .default)
setDividerImage(DividerColorImage, forLeftSegmentState: .normal, rightSegmentState: .normal, barMetrics: .default)
setDividerImage(DividerColorImage, forLeftSegmentState: .selected, rightSegmentState: .selected, barMetrics: .default)
self.apportionsSegmentWidthsByContent = true
layer.borderColor = UIColor.white.cgColor
layer.borderWidth=0
}
}
}
extension UIColor {
publicfuncasImage(_size:CGSize) ->UIImage? {
varresultImage:UIImage? =nil
letrect =CGRect(x:0, y:0, width: size.width, height: size.height)
UIGraphicsBeginImageContextWithOptions(rect.size, false, UIScreen.main.scale)
guard let context = UIGraphicsGetCurrentContext() else {
returnresultImage
}
context.setFillColor(self.cgColor)
context.fill(rect)
resultImage =UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
returnresultImage
}
}
网友评论