Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Errors with custom class UIButton in Swift 4.2. What's new with UIButton?

I have the following code

import UIKit

class CustomButton: UIButton {

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        self.theme_setTitleColor(GlobalPicker.customButtonTextColor, forState: .normal)
        self.theme_setTitleColor(GlobalPicker.customButtonDisabledTextColor, forState: .disabled)
        self.theme_backgroundColor = GlobalPicker.primaryColor

        self.layer.cornerRadius = self.frame.height/4.0
        self.clipsToBounds = true

    }

    override init(frame: CGRect) {
        super.init(frame: frame)

        self.layer.cornerRadius = self.frame.height/2.0
        self.clipsToBounds = true
    }
}

And I'm getting errors when trying to build this code with Xcode 10. The code works just fine with Xcode 9 and Swift 4.0. I was hoping for a seamless transition but apparently that's not what I'm getting.

enter image description here

Is this an Xcode 10 bug? Anyone else running into anything similar?

like image 575
Ethan Allen Avatar asked Dec 07 '25 04:12

Ethan Allen


1 Answers

My guess is that there's an extension somewhere in one of the targets of your project or workspace that messes with UIButton in a way that cripples it somehow. (The fact that this is possible is clear, and I regard it as a bug; see https://bugs.swift.org/browse/SR-2935 and the related duplicates, including https://bugs.swift.org/browse/SR-3228, and mine at https://bugs.swift.org/browse/SR-8010.)

You might be able to slide around the extension by subclassing UIKit.UIButton instead of simple UIButton. For the reason why this works, see the comment discussion in my duplicate bug report. When an extension behaves this way, it overloads methods, and you can distinguish the UIButton that doesn't have the overloads by using the module namespace.

like image 90
matt Avatar answered Dec 08 '25 18:12

matt