簡體   English   中英

如何從UISegmentController中刪除邊框?

[英]how to remove border from UISegmentController?

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

截圖

更新

案例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)

操場

自定義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()

操場

透明邊框

原始答案

答案是否定的
您無法刪除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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM