Search code examples
iosswiftuibuttondlradiobutton

Set DLRadioButton marginWidth between icon and title


Before I upgraded to Swift 4.2 - Xcode 10.1 the DLRadioButton I used had an even spacing between the icon and the title. I never set the spacing and everything worked fine. After the upgrade the icon and the title overlaps

enter image description here

The cocoapod for it says that it uses a default marginWidth of kdefaultmarginwidth

enter image description here

I tried to set the marginWidth in code to anything that would definitely add spacing like 50.0 but the overlap stays. I read somewhere that the kdefaultmarginwidth spacing is 5.0

How can I fix the spacing?

enter image description here

code:

let saleButton: DLRadioButton = {
    let button = DLRadioButton(type: .custom)
    button.translatesAutoresizingMaskIntoConstraints = false
    button.setTitle("Sale", for: .normal)
    button.setTitleColor(UIColor.lightGray, for: .normal)
    button.marginWidth = 50.0 // I tried 5.0, 10.0, 20.0, even 100.0 but nothing
    return button
}()

override func viewDidLoad() {
    super.viewDidLoad()

    view.addSubview(saleButton)
    // constraints get set
}

Solution

  • This is a choppy fix but it works for now because everything else I tried didn't work. Inside the saleButton closure, I had to add 4 empty spaces before I set the string for the title:

    I changed this:

    button.setTitle("Sale", for: .normal)
    

    to this and the overlapping is now gone

    // there are 4 empty spaces in front of the word Sale
    button.setTitle("    Sale", for: .normal)
    

    Here is the image below:

    enter image description here