[英]Change border color of UISegmentedControl
段控制的扩展方法代码。
这是最新的Swift 3.0的有效代码[2017年3月]
创建Extension方法,作为对本机段控件的扩展。
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 ”中
快速尝试:
class MySegmentedControl: UISegmentedControl{
override func awakeFromNib() {
super.awakeFromNib()
for selectView in subviews{
selectView.layer.borderColor = borderColor?.CGColor
selectView.layer.borderWidth = CGFloat(borderWidth)
selectView.layer.cornerRadius = CGFloat(cornerRadius)
selectView.layer.masksToBounds = true
}
}
}
这是因为每个细分都是一个视图,您可以自定义它,但是您想要
尝试这样。
NSArray *arri = [segment subviews];
// Change the tintColor of each subview within the array:
[[arri objectAtIndex:0] setTintColor:[UIColor redColor]];
[[arri objectAtIndex:1] setTintColor:[UIColor greenColor]];
一个简单的解决方案:
//Set default tint color. This will set text color and border color
[[UISegmentedControl appearance] setTintColor:[UIColor whiteColot]];
// Set background image for normal and selected state. This will appear on top of the border and cover the actual border.
[[UISegmentedControl appearance] setBackgroundImage:[UIImage imageNamed:@"btn-blue-shade-1"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setBackgroundImage:[UIImage imageNamed:@"btn-blue-shade-2"] forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
尝试这个
segment.layer.borderColor = [UIColor colorWithRed:<#(CGFloat)#> green:<#(CGFloat)#> blue:<#(CGFloat)#> alpha:<#(CGFloat)#>];
segment.layer.borderWidth = 1.0f;
segment.layer.cornerRadius = 5.0f;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.