2015-12-17 117 views
6

我想刪除UISegmentController的邊框。如果可能。否則在自定義邊框顏色中更改它。如何從UISegmentController中刪除邊界?

Screenshot

+0

你想給自定義邊框給它? –

+0

如果你刪除邊框,那麼它只會顯示文字? – vaibby

+0

http://www.code4app.net/ios/Customizable-control-based-on-UISwitch-and-UISegmentedControl-written-in-Objecti/54460e6be24741786a848c02 – vaibby

回答

3

變色和分段控制的文本嘗試:

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 
+1

在最後一行,你犯了一個錯字。 'objectAtIndex:1'應該是'objectAtIndex:2' – NSNoob

+0

謝謝你「njuri」。你已經給出了最好的解決方案。再次感謝 –

+0

@njuri太好 –

1

更新

案例1 - 中segmentedControl

每個元素的自定義邊框顏色

代碼

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) 

遊樂場

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

的遊樂場

Transparent borders

原來的答案

答案是NO
無法刪除的UISegmentedControl

邊框您可以創建使用UIButton取得成就,你在找什麼自定義控件。

UISegmentedControl狀態,您可以刪除項目之間的分隔在UISegmentedControl,或者您可以更改tintColor(BORDERCOLOR)

enter image description here

+0

「EridB」。其實,你是對的。 –

+0

實際上完全是__not__真,因爲分段控件的外觀可以通過'UIAppearanceProtocol'完全自定義 - 包括去除外邊緣。 – holex

+0

@holex不,你不能「刪除」它。您可以通過將tintColor設置爲clearColor來進行自定義,這會將其設置爲與文本一起不可見,然後設置textAttributes,您可以更改segmentedControl中的項目的textColor。如果這是OP的意思,那麼我會編輯asnwer。 :) – EridB