本文介绍了如何从分段控件中删除边框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何删除分段控件的外边框? 我已经将分频器图像设置为我想要的但现在要按照我的应用程序的模拟我需要一个没有外边框的分段控件。
How do I remove the outside border of a segmented control? I've set the divider image to what I wanted but now to follow the mock of my app I need to have a segmented control without the outer border.
推荐答案您可以使用以下功能删除边框和分隔线。创建UISegmentedControl扩展名:
You can remove both borders and dividers using the below function. Create UISegmentedControl extension:
对于Swift 2.2:
For 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:
For 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分隔符。 (iphone)
感谢 https:// stackoverflow。 com / users / 3921490 / amagain 适用于Swift 3版本。
Thanks to stackoverflow/users/3921490/amagain for Swift 3 version.
更多推荐
如何从分段控件中删除边框
发布评论