Ios 如何在“swift”中设置“UISegmentControl”的边框颜色?
在提问之前,我已经搜索了stackoverflow,找到了一个相关的帖子: 但是我们可以在Ios 如何在“swift”中设置“UISegmentControl”的边框颜色?,ios,swift,uisegmentedcontrol,Ios,Swift,Uisegmentedcontrol,在提问之前,我已经搜索了stackoverflow,找到了一个相关的帖子: 但是我们可以在objective-c语言中看到它 那么我们如何在swift中更改UISegmentControl的边框颜色呢?您只需将代码转换为swift即可 let customSegmentedControl = UISegmentedControl.appearance() customSegmentedControl.setTitleTextAttributes([NSForegroundColorAt
objective-c
语言中看到它
那么我们如何在
swift
中更改UISegmentControl
的边框颜色呢?您只需将代码转换为swift即可
let customSegmentedControl = UISegmentedControl.appearance()
customSegmentedControl.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.red], for: UIControlState.normal)
但我相信你发布的代码改变了字母的实际颜色,而不是外部的颜色。外部颜色称为“色调”,其变化如下:
customSegmentedControl.tintColor = UIColor.blue
编辑:仅用于更改边框
由于SegmentedControl中的每个段都是一个实际的UIView,因此您可以直接访问它们,并根据需要自定义它们,如中所示
或者你可以设置“背景图像”,我相信可以设置为你需要的颜色。(虽然上面的方法似乎不那么复杂)我们可以设置背景图像来达到效果
let customSegmentedControl = UISegmentedControl.appearance()
//customSegmentedControl.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.red], for: UIControlState.normal)
customSegmentedControl.setBackgroundImage(UIImage.init(named: "ni2.png"), for: .normal, barMetrics: .default)
customSegmentedControl.setBackgroundImage(UIImage.init(named: "ni.png"), for: .selected, barMetrics: .default)
更简单的方法:
segmentControl.layer.borderWidth = 1.0
segmentControl.layer.cornerRadius = 5.0
segmentControl.layer.borderColor = UIColor.red.cgColor
segmentControl.layer.masksToBounds = true
添加插座
@IBOutlet weak var customSegment: UISegmentedControl!
然后在viewDidload方法中
customSegment.layer.borderWidth = 1.0
customSegment.layer.cornerRadius = 5.0
customSegment.layer.borderColor = UIColor.red.cgColor
customSegment.layer.masksToBounds = true
很抱歉弄混了,我只想将边框颜色更改为灰色。边框颜色更改为灰色,但内部保持橙色?字母是白色吗?对,只想改变边框颜色,你知道,设置着色颜色边框颜色也会被设置。
customSegment.layer.borderWidth = 1.0
customSegment.layer.cornerRadius = 5.0
customSegment.layer.borderColor = UIColor.red.cgColor
customSegment.layer.masksToBounds = true