Swift:如何从分段控件中删除边框

我如何去除xcode中的分段控件的外部边界? 我已经把divider图像设置为我想要的,但现在要遵循我的应用程序的模拟,我需要有一个没有外边框的分段控制。

您可以使用下面的function删除边框和分隔线。 创buildUISegmentedControl扩展:

对于Swift 2.2:

extension UISegmentedControl { func removeBorders() { setBackgroundImage(imageWithColor(backgroundColor!), forState: .Normal, barMetrics: .Default) setBackgroundImage(imageWithColor(tintColor!), forState: .Selected, barMetrics: .Default) setDividerImage(imageWithColor(UIColor.clearColor()), forLeftSegmentState: .Normal, rightSegmentState: .Normal, barMetrics: .Default) } // create a 1x1 image with this color private func imageWithColor(color: UIColor) -> UIImage { let rect = CGRectMake(0.0, 0.0, 1.0, 1.0) UIGraphicsBeginImageContext(rect.size) let context = UIGraphicsGetCurrentContext() CGContextSetFillColorWithColor(context, color.CGColor); CGContextFillRect(context, rect); let image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image } } 

对于Swift 3:

 extension UISegmentedControl { func removeBorders() { setBackgroundImage(imageWithColor(color: backgroundColor!), for: .normal, barMetrics: .default) setBackgroundImage(imageWithColor(color: tintColor!), for: .selected, barMetrics: .default) setDividerImage(imageWithColor(color: UIColor.clear), forLeftSegmentState: .normal, rightSegmentState: .normal, barMetrics: .default) } // 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! } } 

调用上面的函数。

 segmentedControl.removeBorders() 

参考: 完全删除UISegmentedControl分隔符。 (苹果手机)

感谢https://stackoverflow.com/users/3921490/amagain的Swift 3版本。

这是苏希尔的答案的快速版本,可能会帮助别人。 它帮助了我。 🙂

 extension UISegmentedControl { func removeBorders() { setBackgroundImage(imageWithColor(color: backgroundColor!), for: .normal, barMetrics: .default) setBackgroundImage(imageWithColor(color: tintColor!), for: .selected, barMetrics: .default) setDividerImage(imageWithColor(color: UIColor.clear), forLeftSegmentState: .normal, rightSegmentState: .normal, barMetrics: .default) } // 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! } } 

如果你想保存单元格之间的边界

 extension UISegmentedControl { func removeBorders() { if let backgroundColor = backgroundColor, let backgroundImage = UIImage.imageWithSize(size: CGSize.one_one, color: backgroundColor){ setBackgroundImage(backgroundImage, for: .normal, barMetrics: .default) } if let tintColor = tintColor, let tintImage = UIImage.imageWithSize(size: CGSize.one_one, color: tintColor){ setBackgroundImage(tintImage, for: .selected, barMetrics: .default) setDividerImage(tintImage, forLeftSegmentState: .normal, rightSegmentState: .normal, barMetrics: .default) } } } extension CGSize{ static var one_one: CGSize{ return CGSize(width: 1.0, height: 1.0) } } extension UIImage{ static func imageWithSize(size : CGSize, color : UIColor = UIColor.white) -> UIImage? { var image:UIImage? = nil UIGraphicsBeginImageContext(size) if let context = UIGraphicsGetCurrentContext() { context.setFillColor(color.cgColor) context.addRect(CGRect(origin: CGPoint.zero, size: size)); context.drawPath(using: .fill) image = UIGraphicsGetImageFromCurrentImageContext(); } UIGraphicsEndImageContext() return image } }