gpt4 book ai didi

ios - 更改 UISegmentedControl 的边框颜色

转载 作者:行者123 更新时间:2023-11-28 17:59:24 24 4
gpt4 key购买 nike

我需要为两个部分设置不同的边框颜色。默认情况下,我使用白色作为边框颜色。

谁能帮我解决这个问题?

This UISegmentedcontrol border is what I want

最佳答案

段控件的

扩展方法代码。

这是使用最新 Swift 3.0 [2017 年 3 月]

的工作代码

Extension 方法已创建,作为对 native 段控件的扩展。

extension UISegmentedControl {
func setSegmentStyle() {

let segmentGrayColor = UIColor(red: 0.889415, green: 0.889436, blue:0.889424, alpha: 1.0 )

setBackgroundImage(imageWithColor(color: backgroundColor!), for: .normal, barMetrics: .default)
setBackgroundImage(imageWithColor(color: tintColor!), for: .selected, barMetrics: .default)
setDividerImage(imageWithColor(color: segmentGrayColor), forLeftSegmentState: .normal, rightSegmentState: .normal, barMetrics: .default)
let segAttributes: NSDictionary = [
NSForegroundColorAttributeName: UIColor.gray,
NSFontAttributeName: UIFont(name: "Merriweather-Regular", size: 14)!
]
setTitleTextAttributes(segAttributes as [NSObject : AnyObject], for: UIControlState.normal)
let segAttributesExtra: NSDictionary = [
NSForegroundColorAttributeName: UIColor.white,
NSFontAttributeName: UIFont(name: "Merriweather-Regular", size: 14)!
]
setTitleTextAttributes(segAttributesExtra as [NSObject : AnyObject], for: UIControlState.selected)
selectedSegmentIndex = -1
self.layer.borderWidth = 1.0
self.layer.cornerRadius = 5.0
self.layer.borderColor = segmentGrayColor.cgColor
self.layer.masksToBounds = true
}

// 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!
}
}

此扩展可用于任何使用 UIsegment 控件的代码。

myUISegment.setSegmentStyle()

这将应用所有应用于段控件的样式。

注意:在此示例中,我已经删除了默认边框,更改了字体,在正常、选定模式下设置了不同的颜色,边框明确地制作了一些颜色。

根据您的要求和颜色,您可以相应地更改为绿色或任何自定义颜色,这也在示例“segmentGrayColor”中显示

关于ios - 更改 UISegmentedControl 的边框颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34758590/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com