gpt4 book ai didi

ios - 如何从 UISegmentController 中删除边框?

转载 作者:可可西里 更新时间:2023-11-01 03:45:26 28 4
gpt4 key购买 nike

我想移除 UISegmentController 的边框。如果有可能。否则将其更改为自定义边框颜色。

Screenshot

最佳答案

更新

案例一——自定义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)

Playground

Customize 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()

Playground

Transparent borders

原始答案

答案是NO
您无法移除 UISegmentedControl

的边框

您可以通过使用 UIButton 创建自定义控件来实现您正在寻找的内容。

UISegmentedControl状态下,可以去掉UISegmentedControl中item之间的分隔符,也可以改变tintColor(borderColor)

enter image description here

关于ios - 如何从 UISegmentController 中删除边框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34334967/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com