[英]how to remove border from UISegmentController?
碼
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()
操場
答案是否定的
您無法刪除UISegmentedControl
的邊框
您可以使用UIButton
創建自定義控件來實現您的目標。
在UISegmentedControl
狀態下,您可以刪除UISegmentedControl
項目之間的分隔UISegmentedControl
,或者您可以更改tintColor(borderColor)
要更改分段控件的顏色和文本,請嘗試:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.