Akh*_*iji 0 objective-c uisegmentedcontrol ios
我需要为这两个部分使用不同的边框颜色。默认情况下,我使用白色作为边框颜色。
谁能帮我这个忙吗?
段控制的扩展方法代码。
这是最新的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!
}
}
Run Code Online (Sandbox Code Playgroud)
此扩展可以在使用UIsegment控件的任何代码中使用。
myUISegment.setSegmentStyle()
Run Code Online (Sandbox Code Playgroud)
这将应用应用于细分控件的所有样式。
注意:在此示例中,我删除了默认边框,更改了字体,在正常模式,选定模式下设置了不同的颜色,边框明确地使了一些颜色。
根据您的要求和颜色,您可以相应地更改为绿色或任何自定义颜色,这些颜色也显示在示例“ segmentGrayColor ”中
In swift try this:
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
}
}
}
Run Code Online (Sandbox Code Playgroud)
its because, each Segment is a View, you can custom it However you want
| 归档时间: |
|
| 查看次数: |
10178 次 |
| 最近记录: |