SelectedTintColor of Segment Control is not rounded corner on iOS 13

后端 未结 5 1271
迷失自我
迷失自我 2021-02-06 12:49

Rounded corner is working great on iOS 12 and below, but it\'s broken on iOS 13. I\'ve created a custom Segment control class.

Code:

cla         


        
5条回答
  •  余生分开走
    2021-02-06 12:53

    Swift 5

    If you use a subclass:

    override func layoutSubviews() {
        super.layoutSubviews()
        roundCorners(radius: frame.height / 2)
    
        if #available(iOS 13.0, *) {
            selectedSegmentTintColor = .clear
        } else {
            tintColor = .clear
        }
    
        for (index, subview) in subviews.enumerated() {
            if ((subviews[index] as? UIImageView) != nil) && index == selectedSegmentIndex {
                subview.backgroundColor = .white
                subview.roundCorners(radius: subview.frame.height / 2)
            } else {
                subview.backgroundColor = .clear
            }
        }
    }
    
    private func roundCorners(radius: CGFloat) {
        layer.roundCorners(radius: radius)
        self.clipsToBounds = true
    }
    

    If you use the default segmented control, you just prefix with name of your segmented control:

    mySegmentedControl.selectedSegmentTintColor = .clear
    
    for (index, subview) in mySegmentedControl.subviews.enumerated() {
       .....
    }
    

提交回复
热议问题