How to change the colors of a segment in a UISegmentedControl in iOS 13?

前端 未结 14 871
醉话见心
醉话见心 2020-11-30 18:13

A UISegmentedControl has a new appearance in iOS 13 and existing code to alter the colors of the segmented control no longer work as they did.

Prior to

14条回答
  •  醉话见心
    2020-11-30 18:23

    IOS 13 and Swift 5.0 (Xcode 11.0)Segment Control 100% Working

     if #available(iOS 13.0, *) {
          yoursegmentedControl.backgroundColor = UIColor.black
          yoursegmentedControl.layer.borderColor = UIColor.white.cgColor
          yoursegmentedControl.selectedSegmentTintColor = UIColor.white
          yoursegmentedControl.layer.borderWidth = 1
    
          let titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]    
          yoursegmentedControl.setTitleTextAttributes(titleTextAttributes, for:.normal)
    
          let titleTextAttributes1 = [NSAttributedString.Key.foregroundColor: UIColor.black]
          yoursegmentedControl.setTitleTextAttributes(titleTextAttributes1, for:.selected)
      } else {
                  // Fallback on earlier versions
    }
    

提交回复
热议问题