【发布时间】:2015-12-18 00:58:05
【问题描述】:
【问题讨论】:
-
你想给它自定义边框吗??
-
如果你删除边框那么它只会显示文本?
-
你是要去掉边框还是要自定义??
-
是的,我想删除边框。如果可能的话。
【问题讨论】:
代码
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)
游乐场
代码
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()
游乐场
答案是NO
你不能移除UISegmentedControl的边框
您可以通过使用UIButtons 创建自定义控件来实现您想要的。
UISegmentedControl状态下,可以去掉UISegmentedControl中item之间的分隔线,也可以改变tintColor(borderColor)
【讨论】:
UIAppearanceProtocol 完全自定义——包括移除外边缘。
UIAppearanceProtocol 完成——如果你想更新你的答案,请这样做并向他展示它实际上是如何工作的,但是 OP 没有提出这个问题,因为他想从谁那里得到答案 不知道如何提交。
UIApperanceProtocol 需要 UI_APPEARANCE_SELECTOR 选项,这在早期版本的 iOS 中一直是一个流行的问题。因此,在说出UIAppearanceProtocol 一致性之前,您需要检查该选项。我总是倾向于跟进类的公共属性和方法,而不是进行可能导致错误或意外行为的自定义。无论如何感谢您的观点,我已经编辑了答案以符合 OP 的意图:)
要更改分段控件的颜色和文本,请尝试:
Objective-C:
NSArray *array = [segmentedControl subviews];
[[array objectAtIndex:2] setTintColor:[UIColor redColor]];
[[array objectAtIndex:1] setTintColor:[UIColor greenColor]];
[[array objectAtIndex:0] setTintColor:[UIColor blueColor]];
斯威夫特:
let array = segmentedControl.subviews
array[2].tintColor = UIColor.redColor()
array[1].tintColor = UIColor.greenColor()
array[0].tintColor = UIColor.blueColor()
请注意,subviews 与用户界面的顺序相反。
你可以用同样的方式自定义边框:
let array = segmentedControl.subviews
array[0].layer.borderWidth = 5 // change thickness of border
array[0].layer.cornerRadius = 4 //change corner radius
【讨论】:
objectAtIndex:1 应该是 objectAtIndex:2