SelectedTintColor of Segment Control is not rounded corner on iOS 13

后端 未结 5 1268
迷失自我
迷失自我 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:54

    Similar to other solution I have Following Sub-Class Segment control UISegmentedControl

    Which gives following result -

    class OYSegmentControl: UISegmentedControl {
      
      override func layoutSubviews(){
        super.layoutSubviews()
        
        let segmentStringSelected: [NSAttributedString.Key : Any] = [
          NSAttributedString.Key.font : UIFont.fontActionLabel(ofSize: 14.0),
          NSAttributedString.Key.foregroundColor : UIColor.white
        ]
        
        let segmentStringHighlited: [NSAttributedString.Key : Any] = [
          NSAttributedString.Key.font : UIFont.fontActionLabel(ofSize: 14.0),
          NSAttributedString.Key.foregroundColor : #colorLiteral(red: 0.5567105412, green: 0.5807551742, blue: 0.6022000909, alpha: 1)
        ]
        
        setTitleTextAttributes(segmentStringHighlited, for: .normal)
        setTitleTextAttributes(segmentStringSelected, for: .selected)
        setTitleTextAttributes(segmentStringHighlited, for: .highlighted)
        
        layer.masksToBounds = true
        
        if #available(iOS 13.0, *) {
          selectedSegmentTintColor = #colorLiteral(red: 0, green: 0.861200273, blue: 0.67304039, alpha: 1)
        } else {
          tintColor = #colorLiteral(red: 0, green: 0.861200273, blue: 0.67304039, alpha: 1)
        }
        
        backgroundColor = #colorLiteral(red: 0.9191747308, green: 0.9334954619, blue: 0.9506797194, alpha: 1)
        
        //corner radius
        let cornerRadius = bounds.height / 2
        let maskedCorners: CACornerMask = [.layerMinXMinYCorner, .layerMinXMaxYCorner, .layerMaxXMinYCorner, .layerMaxXMaxYCorner]
        //background
        clipsToBounds = true
        layer.cornerRadius = cornerRadius
        layer.maskedCorners = maskedCorners
    
        let foregroundIndex = numberOfSegments
        if subviews.indices.contains(foregroundIndex),
          let foregroundImageView = subviews[foregroundIndex] as? UIImageView {
          foregroundImageView.image = UIImage()
          foregroundImageView.clipsToBounds = true
          foregroundImageView.layer.masksToBounds = true
          foregroundImageView.backgroundColor = #colorLiteral(red: 0, green: 0.861200273, blue: 0.67304039, alpha: 1)
          
          foregroundImageView.layer.cornerRadius = bounds.height / 2 + 5
          foregroundImageView.layer.maskedCorners = maskedCorners
        }
      }
      
      override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        return false
      }
      
    }
    

    Language: Swift 5.1

    NOTE: This only works if you have outlet / frame set from Storyboard. Frame from code will cause issues. The extra 5 px on cornerRadius,a hack to make it better round rect. I ended up using - https://github.com/alokc83/MASegmentedControl as my use-case was from Code only view.

提交回复
热议问题