分段控件,更改色调不透明度,但不更改边框 [英] Segmented control, change tint opacity, but not border
本文介绍了分段控件,更改色调不透明度,但不更改边框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 Swift
中,如何更改所选索引的色调不透明度,而不更改整个控件的边框?
这会改变整个控件的颜色和不透明度:
sessionTypeSegmentedControl.tintColor = UIColor(red: 140/255, green: 140/255, blue: 140/255, alpha: 0.1)
接下来我试过:
sessionTypeSegmentedControl.layer.borderColor = UIColor(red: 0/255, green: 0/255, blue: 0/255, alpha: 1.0).cgColor
但这对边界没有影响.
我想更改此图像中显示的蓝色背景颜色不透明度.在最左边.
In Swift
, how do I change the tint opacity of the selected index, but not the border of the whole control?
This changes the color and opacity of the whole control:
sessionTypeSegmentedControl.tintColor = UIColor(red: 140/255, green: 140/255, blue: 140/255, alpha: 0.1)
Followed by this I tried:
sessionTypeSegmentedControl.layer.borderColor = UIColor(red: 0/255, green: 0/255, blue: 0/255, alpha: 1.0).cgColor
But this has no effect on the border.
EDIT:
I want to change the blue background color opacity shown in this image. On the far left.
http://i.stack.imgur.com/GgUwN.png
So the expected result would be:
解决方案
I found this solution... Swift 3 Xcode 8
@IBAction func valueChanged(_ sender: UISegmentedControl) {
for (index,element) in segment.subviews.enumerated() {
if index != sender.selectedSegmentIndex {
element.tintColor = UIColor.red
element.alpha = 0.5
}else {
element.tintColor = UIColor.red
element.alpha = 1
}
}
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
for (index,element) in segment.subviews.enumerated() {
if index != segment.selectedSegmentIndex {
element.tintColor = UIColor.red
element.alpha = 0.5
}else {
element.tintColor = UIColor.red
element.alpha = 1
}
}
segment.layer.cornerRadius = 5
segment.layer.borderColor = UIColor.black.cgColor
segment.layer.borderWidth = 1
segment.clipsToBounds = true
}
这篇关于分段控件,更改色调不透明度,但不更改边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文