ios - Segment Control 的 SelectedTintColor 在 iOS 13 上不是圆角

标签 ios swift4 uisegmentedcontrol ios13

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

代码:

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

   }
}

我已经浏览了这篇文章 - How to change the colors of a segment in a UISegmentedControl in iOS 13?
但我找不到任何解决方案。

截图:
enter image description here

最佳答案

我在 iOS 13 上遇到了同样的问题。然后我深入研究了它的 View 层次结构,然后我发现它有多个 subview 。所以我为 iOS 13 做了一个技巧。你必须对 iOS 13 做以下更改 -

  • 更改 selectedSegmentTintColorClear - self.selectedSegmentTintColor = .clear
  • 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
                }
    
            }
        }
    

  • 我希望它会帮助你。

    关于ios - Segment Control 的 SelectedTintColor 在 iOS 13 上不是圆角,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58315497/

    相关文章:

    cocoa-touch - 我可以以编程方式修改选择的 UITableViewCell 吗?

    ios - 在tableview中删除一行

    ios - Xcode 4.6 中 UISegmentedControl 的本地化中断

    swift - 无法从 Firestore 数据库加载分段控制 TableView 中的数据

    ios - 如何检查数组中是否已经存在自定义对象?

    ios - 如何用动画更改 UISegmentedControl 索引?

    iOS 应用程序开始在 4"screen like on 3.5"上运行

    javascript - JS区分iPhone上的点击和滚动

    objective-c - 确定弱引用对象是否已被释放

    swift - 转换为 Swift 4 后,Xcode 项目不断崩溃, "Class implemented in both..."