UISegmentedControl Borders/Highlighted 状态
UISegmentedControl Borders/Highlighted State
我目前在我的 VC 中有一个 uisegmentedcontrol 元素,并且已经按照下面的主题设法删除了边框。虽然它工作正常,但一旦选择 uisegmentedcontrol 部分就会变成全白,包括图标,有什么办法可以修改吗?所以所选部分的背景将是我指定的任何颜色并且图标变为白色。提前感谢任何可能提供帮助的人。
讨论帖提到:
这是您可以更改 UISegmentControl
中选定指标的背景颜色的方法
extension UISegmentedControl{
func removeBorders() {
setBackgroundImage(imageWithColor(color: .clear), for: .normal, barMetrics: .default)
setBackgroundImage(imageWithColor(color: .gray), for: .selected, barMetrics: .default)
setDividerImage(imageWithColor(color: UIColor.clear), forLeftSegmentState: .normal, rightSegmentState: .normal, barMetrics: .default)
}
// create a 1x1 image with this color
private func imageWithColor(color: UIColor) -> UIImage {
let rect = CGRect(x: 0.0, y: 0.0, width: 1.0, height: 1.0)
UIGraphicsBeginImageContext(rect.size)
let context = UIGraphicsGetCurrentContext()
context!.setFillColor(color.cgColor);
context!.fill(rect);
let image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image!
}
}
正如您在 removeBorders
函数中看到的,您正在为 normal
和 selected
状态设置基于颜色的背景图像。所以在这里你可以使用任何你喜欢的颜色。
我目前在我的 VC 中有一个 uisegmentedcontrol 元素,并且已经按照下面的主题设法删除了边框。虽然它工作正常,但一旦选择 uisegmentedcontrol 部分就会变成全白,包括图标,有什么办法可以修改吗?所以所选部分的背景将是我指定的任何颜色并且图标变为白色。提前感谢任何可能提供帮助的人。
讨论帖提到:
这是您可以更改 UISegmentControl
extension UISegmentedControl{
func removeBorders() {
setBackgroundImage(imageWithColor(color: .clear), for: .normal, barMetrics: .default)
setBackgroundImage(imageWithColor(color: .gray), for: .selected, barMetrics: .default)
setDividerImage(imageWithColor(color: UIColor.clear), forLeftSegmentState: .normal, rightSegmentState: .normal, barMetrics: .default)
}
// create a 1x1 image with this color
private func imageWithColor(color: UIColor) -> UIImage {
let rect = CGRect(x: 0.0, y: 0.0, width: 1.0, height: 1.0)
UIGraphicsBeginImageContext(rect.size)
let context = UIGraphicsGetCurrentContext()
context!.setFillColor(color.cgColor);
context!.fill(rect);
let image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image!
}
}
正如您在 removeBorders
函数中看到的,您正在为 normal
和 selected
状态设置基于颜色的背景图像。所以在这里你可以使用任何你喜欢的颜色。