段控制的SelectedTintColor在iOS 13上不是圆角 [英] SelectedTintColor of Segment Control is not rounded corner on iOS 13

查看:51
本文介绍了段控制的SelectedTintColor在iOS 13上不是圆角的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

圆角在iOS 12及以下版本上运行良好,但在iOS 13上已损坏.我创建了一个自定义的Segment控件类.

代码:

  class SegmentedControl:UISegmentedControl {覆盖func layoutSubviews(){super.layoutSubviews()layer.cornerRadius = self.bounds.size.height/2.0layer.borderColor = UIColor(红色:170.0/255.0,绿色:170.0/255.0,蓝色:170.0/255.0,alpha:1.0).cgColorlayer.borderWidth = 1.0layer.masksToBounds = trueclipsToBounds = true}} 

我已经看过这篇文章-

解决方案

我在iOS 13上遇到了同样的问题.然后,我深入研究了它的视图层次结构,然后发现它具有多个子视图.因此,我为iOS 13制作了一个技巧.您必须对iOS 13进行以下更改-

  1. selectedSegmentTintColor 更改为 Clear - self.selectedSegmentTintColor = .clear
  2. layoutSubviews -

    中添加以下代码段

      for i in 0 ... subviews.count-1 {如果让subview = subviews [i]为?UIImageView {如果i == self.selectedSegmentIndex {subview.backgroundColor = UIColor(红色:170.0/255.0,绿色:170.0/255.0,蓝色:170.0/255.0,alpha:1.0)}别的{subview.backgroundColor = .clear}}} 

希望它能对您有所帮助.

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:

class SegmentedControl: UISegmentedControl {
    override func layoutSubviews() {
      super.layoutSubviews()
      layer.cornerRadius = self.bounds.size.height / 2.0
      layer.borderColor = UIColor(red: 170.0/255.0, green: 170.0/255.0, blue: 170.0/255.0, alpha: 1.0).cgColor
      layer.borderWidth = 1.0
      layer.masksToBounds = true
      clipsToBounds = true

   }
}

I've gone through this post - How to change the colors of a segment in a UISegmentedControl in iOS 13? but I couldn't get any solution.

Screenshot:

解决方案

I was facing the same issue on iOS 13. Then I dug into its view hierarchy then I found it has multiple subviews. So I made a trick for iOS 13. You have to do following changes for iOS 13 -

  1. ChangeselectedSegmentTintColor to Clear - self.selectedSegmentTintColor = .clear
  2. Add following code snippet inside layoutSubviews -

    for i in 0...subviews.count - 1{
    
            if let subview = subviews[i] as? UIImageView{
    
                if i == self.selectedSegmentIndex {
    
                    subview.backgroundColor = UIColor(red: 170.0/255.0, green: 170.0/255.0, blue: 170.0/255.0, alpha: 1.0)
    
                }else{
    
                    subview.backgroundColor = .clear
                }
    
            }
        }
    

I hope it will help you.

这篇关于段控制的SelectedTintColor在iOS 13上不是圆角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆