1

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.

I'd like to put these back to what they were before and I'm part way there with the following code, within AppDelegate.didFinishLaunchingWithOptions

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)

My thanks to this question for some pointers here. However, there are some things that haven't been restored, which I believe are to do with the layer - something like this:

layer.borderWidth = 1
layer.borderColor = UIColor.blue
layer.masksToBounds = true
layer.cornerRadius = 2

(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:

UISegmentedControl.appearance().layer.borderWidth = 1
UISegmentedControl.appearance().layer.borderColor = uicolor_normal.cgColor
UISegmentedControl.appearance().layer.masksToBounds = true
UISegmentedControl.appearance().layer.cornerRadius = 2

Unfortunately, that doesn't seem to work. Perhaps because it needs to wait until it's been drawn? I did come across some slightly clunky code that offered to do something similar to do with the UINavigationBar, but this didn't work either:

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 = ""

Can anybody offer some advice on how to affect the layer of all segment controls, without adding/changing any code within my ViewControllers?

Many thanks!

Ahmed Elgendy
  • 1,670
  • 2
  • 12
  • 17
Ben
  • 4,707
  • 5
  • 34
  • 55

0 Answers0