UISegmentedControl iOS 13 清色
UISegmentedControl iOS 13 clear color
在iOS12,得到一个边框清晰,分隔线清晰的UISegmentedControl,一切都很简单。我所做的就是这样:
settingControl.tintColor = .clear
let font = myFont
let boldfont = myBoldFont
settingControl.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor.white, NSAttributedString.Key.font:font], for: .normal)
settingControl.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor.red, NSAttributedString.Key.font:boldfont], for: .selected)
然后 UISegmentedControl 是全透明颜色(分隔线、背景、边框)
但是在iOS13,我还不是很清楚。我可以设置
settingControl.selectedSegmentTintColor = UIColor.clear
但是还是没有清除backgroundColor和分割线
我试过将 backgroundColor 设置为清除,但没有效果。
settingControl.backgroundColor = UIColor.clear
我也试过设置清晰的图片,还是不行:
public extension UIImage {
/**
Returns image with size 1x1px of certain color.
*/
class func imageWithColor(color : UIColor) -> UIImage? {
let rect = CGRect(x: 0, y: 0, width: 1, height: 1)
UIGraphicsBeginImageContext(rect.size)
let context = UIGraphicsGetCurrentContext()
context?.setFillColor(color.cgColor)
context?.fill(rect)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}
然后是这个:
let clearImage = UIImage.imageWithColor(color: UIColor.clear)
settingControl.setDividerImage(clearImage, forLeftSegmentState: .normal, rightSegmentState: .normal, barMetrics: .default)
settingControl.setBackgroundImage(clearImage, for: .normal, barMetrics: .default)
settingControl.setBackgroundImage(clearImage, for: .selected, barMetrics: .default)
这是 iOS 12 上的样子。在 iOS 13 上,这似乎是不可能的。
这是在 iOS13 中复制普通分段控件的方法:
import UIKit
import PlaygroundSupport
class PlainSegmentedControl: UISegmentedControl {
override init(items: [Any]?) {
super.init(items: items)
setup()
}
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// Used for the unselected labels
override var tintColor: UIColor! {
didSet {
setTitleTextAttributes([.foregroundColor: tintColor!, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 13, weight: .regular)], for: .normal)
}
}
// Used for the selected label
override var selectedSegmentTintColor: UIColor? {
didSet {
setTitleTextAttributes([.foregroundColor: selectedSegmentTintColor ?? tintColor!, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 13, weight: .regular)], for: .selected)
}
}
private func setup() {
backgroundColor = .clear
// Use a clear image for the background and the dividers
let tintColorImage = UIImage(color: .clear, size: CGSize(width: 1, height: 32))
setBackgroundImage(tintColorImage, for: .normal, barMetrics: .default)
setDividerImage(tintColorImage, forLeftSegmentState: .normal, rightSegmentState: .normal, barMetrics: .default)
// Set some default label colors
setTitleTextAttributes([.foregroundColor: UIColor.black, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 13, weight: .regular)], for: .normal)
setTitleTextAttributes([.foregroundColor: tintColor!, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 13, weight: .regular)], for: .selected)
}
}
这里有一些测试代码可以放在 playground 中:
// Create a dark green view as a test background
let bg = UIView(frame: CGRect(x: 0, y: 0, width: 400, height: 100))
bg.backgroundColor = UIColor(red: 0.224, green: 0.408, blue: 0.467, alpha: 1)
// The plain segmented control
let seg = PlainSegmentedControl(items: ["Number One", "Number Two", "Number Three"])
seg.tintColor = .white
seg.selectedSegmentTintColor = .green
seg.selectedSegmentIndex = 0
bg.addSubview(seg)
PlaygroundPage.current.liveView = bg
这是 UIImage
从颜色创建尺寸图片的扩展:
extension UIImage {
convenience init(color: UIColor, size: CGSize) {
UIGraphicsBeginImageContextWithOptions(size, false, 1)
color.set()
let ctx = UIGraphicsGetCurrentContext()!
ctx.fill(CGRect(origin: .zero, size: size))
let image = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
self.init(data: image.pngData()!)!
}
}
要使 UISegmentedControl 的背景透明,您可以试试这个:
segmentedControl.backgroundColor = .clear
segmentedControl.setBackgroundImage(UIImage(), for: .normal, barMetrics: .default)
在iOS12,得到一个边框清晰,分隔线清晰的UISegmentedControl,一切都很简单。我所做的就是这样:
settingControl.tintColor = .clear
let font = myFont
let boldfont = myBoldFont
settingControl.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor.white, NSAttributedString.Key.font:font], for: .normal)
settingControl.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor.red, NSAttributedString.Key.font:boldfont], for: .selected)
然后 UISegmentedControl 是全透明颜色(分隔线、背景、边框)
但是在iOS13,我还不是很清楚。我可以设置
settingControl.selectedSegmentTintColor = UIColor.clear
但是还是没有清除backgroundColor和分割线
我试过将 backgroundColor 设置为清除,但没有效果。
settingControl.backgroundColor = UIColor.clear
我也试过设置清晰的图片,还是不行:
public extension UIImage {
/**
Returns image with size 1x1px of certain color.
*/
class func imageWithColor(color : UIColor) -> UIImage? {
let rect = CGRect(x: 0, y: 0, width: 1, height: 1)
UIGraphicsBeginImageContext(rect.size)
let context = UIGraphicsGetCurrentContext()
context?.setFillColor(color.cgColor)
context?.fill(rect)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}
然后是这个:
let clearImage = UIImage.imageWithColor(color: UIColor.clear)
settingControl.setDividerImage(clearImage, forLeftSegmentState: .normal, rightSegmentState: .normal, barMetrics: .default)
settingControl.setBackgroundImage(clearImage, for: .normal, barMetrics: .default)
settingControl.setBackgroundImage(clearImage, for: .selected, barMetrics: .default)
这是 iOS 12 上的样子。在 iOS 13 上,这似乎是不可能的。
这是在 iOS13 中复制普通分段控件的方法:
import UIKit
import PlaygroundSupport
class PlainSegmentedControl: UISegmentedControl {
override init(items: [Any]?) {
super.init(items: items)
setup()
}
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// Used for the unselected labels
override var tintColor: UIColor! {
didSet {
setTitleTextAttributes([.foregroundColor: tintColor!, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 13, weight: .regular)], for: .normal)
}
}
// Used for the selected label
override var selectedSegmentTintColor: UIColor? {
didSet {
setTitleTextAttributes([.foregroundColor: selectedSegmentTintColor ?? tintColor!, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 13, weight: .regular)], for: .selected)
}
}
private func setup() {
backgroundColor = .clear
// Use a clear image for the background and the dividers
let tintColorImage = UIImage(color: .clear, size: CGSize(width: 1, height: 32))
setBackgroundImage(tintColorImage, for: .normal, barMetrics: .default)
setDividerImage(tintColorImage, forLeftSegmentState: .normal, rightSegmentState: .normal, barMetrics: .default)
// Set some default label colors
setTitleTextAttributes([.foregroundColor: UIColor.black, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 13, weight: .regular)], for: .normal)
setTitleTextAttributes([.foregroundColor: tintColor!, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 13, weight: .regular)], for: .selected)
}
}
这里有一些测试代码可以放在 playground 中:
// Create a dark green view as a test background
let bg = UIView(frame: CGRect(x: 0, y: 0, width: 400, height: 100))
bg.backgroundColor = UIColor(red: 0.224, green: 0.408, blue: 0.467, alpha: 1)
// The plain segmented control
let seg = PlainSegmentedControl(items: ["Number One", "Number Two", "Number Three"])
seg.tintColor = .white
seg.selectedSegmentTintColor = .green
seg.selectedSegmentIndex = 0
bg.addSubview(seg)
PlaygroundPage.current.liveView = bg
这是 UIImage
从颜色创建尺寸图片的扩展:
extension UIImage {
convenience init(color: UIColor, size: CGSize) {
UIGraphicsBeginImageContextWithOptions(size, false, 1)
color.set()
let ctx = UIGraphicsGetCurrentContext()!
ctx.fill(CGRect(origin: .zero, size: size))
let image = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
self.init(data: image.pngData()!)!
}
}
要使 UISegmentedControl 的背景透明,您可以试试这个:
segmentedControl.backgroundColor = .clear
segmentedControl.setBackgroundImage(UIImage(), for: .normal, barMetrics: .default)