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

编程入门 行业动态 更新时间:2024-10-28 00:24:31
本文介绍了如何从分段控件中删除边框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何删除分段控件的外边框? 我已经将分频器图像设置为我想要的但现在要按照我的应用程序的模拟我需要一个没有外边框的分段控件。

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.

更多推荐

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

本文发布于:2023-08-05 17:08:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1306861.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:边框   控件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!