答案 0 :(得分:5)
扩展方法 代码。
这是使用最新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 ”中显示
答案 1 :(得分:4)
快速尝试一下:
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
}
}
}
因为,每个Segment都是一个View,你可以自定义它但是你想要
答案 2 :(得分:1)
试试这样。
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]];
答案 3 :(得分:1)
一个简单的解决方案:
//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];
答案 4 :(得分:-1)
试试这个
segment.layer.borderColor = [UIColor colorWithRed:<#(CGFloat)#> green:<#(CGFloat)#> blue:<#(CGFloat)#> alpha:<#(CGFloat)#>];
segment.layer.borderWidth = 1.0f;
segment.layer.cornerRadius = 5.0f;