How to set backgroundColor of UISegmentedControl to white in iOS 13

后端 未结 4 1237
温柔的废话
温柔的废话 2021-02-05 07:08

iOS 13 introduced some changes to the UISegmentedControl including a really nice animation when switching the selected segment. However I\'m noticing that it\'s not displaying t

4条回答
  •  悲&欢浪女
    2021-02-05 08:01

    Works for me (Swift 5).

    let background = myColors.background
    let selectedColor = myColors.foreground
    
    if #available(iOS 13.0, *)
    {
        segmentedControl.tintColor = background
        segmentedControl.backgroundColor = background
        segmentedControl.selectedSegmentTintColor = selectedColor
        segmentedControl.setTitleTextAttributes([.foregroundColor: selectedColor as Any], for: .normal)
        segmentedControl.setTitleTextAttributes([.foregroundColor: background as Any], for: .selected)
    }
    else
    {
        segmentedControl.tintColor = background
        segmentedControl.backgroundColor = selectedColor
        segmentedControl.layer.cornerRadius = 4
    }
    

提交回复
热议问题