Remove UISegmentedControl separators completely. (iphone)

前端 未结 9 1744
Happy的楠姐
Happy的楠姐 2020-12-05 05:30

Is there a way to completely remove the line separating the two segments in a UISegmentedControl?

Setting the segmentedControlStyle is not helping.

9条回答
  •  粉色の甜心
    2020-12-05 05:48

    I combined the previous answers into a Swift extension.

    I wanted to remove all borders and dividers. This code just uses the normal segment colors, e.g. tint and background for selected / deselected.

    Call

    segmentedControl.removeBorders()
    

    Here's the extension

    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
        }
    }
    

提交回复
热议问题