title: iOS13 Segment Control Customization
body: <p>Having just updated to iOS 13 and Xcode 11, I can see that the segment controls in our app have been replaced and our nice colours and style are now being ignored. </p>
  
  <p>I'd like to put these back to what they were before and I'm part way there with the following code, within <code>AppDelegate.didFinishLaunchingWithOptions</code></p>
  
  <pre><code>UISegmentedControl.appearance().setTitleTextAttributes(segmentAttributes , for: .normal)
  UISegmentedControl.appearance().setTitleTextAttributes(segmentSelectedAttributes , for: .selected)
  UISegmentedControl.appearance().setBackgroundImage(UIImage.imageWithColor(UIColor.white, size: CGSize(width: 1, height: 1)), for: .normal, barMetrics: .default)
  UISegmentedControl.appearance().setBackgroundImage(UIImage.imageWithColor(uicolor_normal, size: CGSize(width: 1, height: 1)), for: .selected, barMetrics: .default)
  UISegmentedControl.appearance().setDividerImage(UIImage.imageWithColor(uicolor_normal, size: CGSize(width: 1, height: 1)),forLeftSegmentState: .normal, rightSegmentState: .normal, barMetrics: .default)
  </code></pre>
  
  <p>My thanks to <a href="https://stackoverflow.com/questions/56436559/how-to-change-the-colors-of-a-segment-in-a-uisegmentedcontrol-in-ios-13">this question</a> for some pointers here. However, there are some things that haven't been restored, which I believe are to do with the <code>layer</code> - something like this:</p>
  
  <pre><code>layer.borderWidth = 1
  layer.borderColor = UIColor.blue
  layer.masksToBounds = true
  layer.cornerRadius = 2
  </code></pre>
  
  <p>(The border width, border colour and corner radius)
  Now the question linked above shows these things being added, but I'd like to place this code in a single place (AppDelegate.didFinishLaunchingWithOptions) to keep my code clean. So I tried adding these:</p>
  
  <pre><code>UISegmentedControl.appearance().layer.borderWidth = 1
  UISegmentedControl.appearance().layer.borderColor = uicolor_normal.cgColor
  UISegmentedControl.appearance().layer.masksToBounds = true
  UISegmentedControl.appearance().layer.cornerRadius = 2
  </code></pre>
  
  <p>Unfortunately, that doesn't seem to work. Perhaps because it needs to wait until it's been drawn? I did come across some <a href="https://stackoverflow.com/questions/31756230/uinavigationbar-set-custom-shadow-in-appdelegate-swift">slightly clunky code</a> that offered to do something similar to do with the UINavigationBar, but this didn't work either:</p>
  
  <pre><code>extension UINavigationBar {
  
    var castShadow : String {
      get { return "anything fake" }
      set {
          self.layer.shadowOffset = CGSizeMake(0, 3)
          self.layer.shadowRadius = 3.0
          self.layer.shadowColor = UIColor.yellowColor().CGColor
          self.layer.shadowOpacity = 0.7
  
      }
    }
  
  }
  // below should go in didFinishLaunchingWithOptions...
  UINavigationBar.appearance().castShadow = ""
  </code></pre>
  
  <p>Can anybody offer some advice on how to affect the layer of all segment controls, without adding/changing any code within my ViewControllers?</p>
  
  <p>Many thanks!</p>