swiftborderuiswitch

Specific round border of IOS switch


Im trying to specifically get this border color for a switch in IOS :

enter image description here

But I have tried implementing different answers on this site and these are some of the results :

enter image description here enter image description here enter image description here

This is the code I have until now :

let createSwitch: () -> UISwitch = {
    let swt = UISwitch()
    swt.layer.cornerRadius = swt.frame.height / 2
    swt.layer.borderColor = UIColor.darkGray.cgColor
    swt.layer.borderWidth = 1
    swt.translatesAutoresizingMaskIntoConstraints = false
    return swt
}

But although it gets close, it is not exactly what I need. In the second sample, I almost got it but the border is overlapping the darker background for some reason.

What is wrong here?


Solution

  • If you're familiar with using Cocoapods, I suggest using PWSwitch. It is a UISwitch implementation that is far more customizable, including things like border color.

    I've used it in a number of my projects and its very easy to use.

    Link: https://github.com/Shaninnik/PWSwitch