如何从UISegmentController中删除边框?

Abd*_*eed 6 uisegmentedcontrol ios

我想删除UISegmentController的边框.如果有可能.否则在自定义边框颜色中更改它.

截图

E-R*_*die 5

更新资料

情况1-自定义segmentedControl中每个元素的borderColor

extension UIView {
    ///Add border color with corners
    func addBorderWithColor(color: UIColor, roundingCorners: UIRectCorner) {
        self.layer.borderWidth = 1
        self.layer.borderColor = color.CGColor
        self.addRoundingCorners(roundingCorners)
    }

    ///Use corner radius depending on UIRectCorner
    private func addRoundingCorners(roundingCorners: UIRectCorner) {
        let path = UIBezierPath(roundedRect:self.bounds, byRoundingCorners:roundingCorners, cornerRadii: CGSizeMake(4, 4))

        let maskLayer = CAShapeLayer()
        maskLayer.path = path.CGPath
        self.layer.mask = maskLayer
    }
}

let segmentedControl = UISegmentedControl(items: ["Red", "Green", "Blue"])

segmentedControl.subviews[0].addBorderWithColor(UIColor.blueColor(), roundingCorners: [.TopRight, .BottomRight])
segmentedControl.subviews[1].addBorderWithColor(UIColor.greenColor(), roundingCorners: [])
segmentedControl.subviews[2].addBorderWithColor(UIColor.redColor(), roundingCorners: [.TopLeft, .BottomLeft])

segmentedControl.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.blackColor()], forState: UIControlState.Normal)
Run Code Online (Sandbox Code Playgroud)

操场

自定义borderColor

案例2-摆脱国界

let segmentedControl = UISegmentedControl(items: ["Red", "Green", "Blue"])

//Change Text Attributes (Changing textColor to black)
//**Be sure to manage all the UIControlState for these attributes if you need to customize this for other states
segmentedControl.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.blackColor()], forState: UIControlState.Normal)

//Change tintColor to clear, in order to set border invisible
segmentedControl.tintColor = UIColor.clearColor()
Run Code Online (Sandbox Code Playgroud)

操场

透明边框

原始答案

答案是否定的。
您不能删除边框UISegmentedControl

您可以使用来创建自定义控件,UIButton以实现所需的功能。

在状态下UISegmentedControl,您可以删除中的项目之间的分隔线UISegmentedControl,也可以更改tintColor(borderColor)

在此处输入图片说明


Jur*_*oga 3

要更改分段控件的颜色和文本,请尝试:

目标-C

NSArray *array = [segmentedControl subviews];

[[array objectAtIndex:2] setTintColor:[UIColor redColor]];
[[array objectAtIndex:1] setTintColor:[UIColor greenColor]];    
[[array objectAtIndex:0] setTintColor:[UIColor blueColor]];
Run Code Online (Sandbox Code Playgroud)

斯威夫特

let array = segmentedControl.subviews
array[2].tintColor = UIColor.redColor()
array[1].tintColor = UIColor.greenColor()
array[0].tintColor = UIColor.blueColor()
Run Code Online (Sandbox Code Playgroud)

请注意,subviews它们的顺序与用户界面相反。

您可以用同样的方式自定义边框:

let array = segmentedControl.subviews
array[0].layer.borderWidth = 5 // change thickness of border
array[0].layer.cornerRadius = 4 //change corner radius
Run Code Online (Sandbox Code Playgroud)