简体   繁体   中英

Swift: Target not getting called on a subclassed UIButton

CustomButton.swift

class CustomButton: UIButton {

    override func draw(_ rect: CGRect) {
        //drawing code
    }
}

ViewController.swift

let testCustom = CustomButton()
testCustom.draw(CGRect(x: 0, y: 0, width: 0, height: 0))
testCustom.isUserInteractionEnabled = true
testCustom.addTarget(self, action: #selector(Start(_:)), for: .touchUpInside)
self.view.addSubview(testCustom)

@objc func Start(_ sender: CustomButton) {
    print("pressed start")
}

The button appears on screen but the function does not get called upon pressing the button. Any ideas why?

I also tried the function and addTarget code within the CustomButton.swift but couldn't get that to trigger either.

Thanks for any help!

The following is a simple example of how to instantiate a UIButton subclass in a view controller ( UIViewController ). It's tested under Swift 4.2.

// Subclassing UIButton //
import UIKit

class MyButton: UIButton {
    var tintColor0: UIColor!
    var tintColor1: UIColor!
    var borderColor: UIColor!
    var backColor: UIColor!
    var cornerRadius: CGFloat!

    required init(frame: CGRect, tintColor0: UIColor, tintColor1: UIColor, borderColor: UIColor, backColor: UIColor, cornerRadius: CGFloat, titleString: String) {
        super.init(frame: frame)

        self.tintColor0 = tintColor0
        self.tintColor1 = tintColor1
        self.borderColor = borderColor
        self.backColor = backColor
        self.cornerRadius = cornerRadius
        self.setTitle(titleString, for: .normal)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func draw(_ rect: CGRect) {
        super.draw(rect)

        self.setTitleColor(tintColor0, for: .normal)
        self.setTitleColor(tintColor1, for: .highlighted)
        self.layer.borderColor = borderColor.cgColor
        self.layer.cornerRadius = cornerRadius
        self.layer.borderWidth = 1.0
        self.layer.backgroundColor = backColor.cgColor
    }
}

// View controller //
import UIKit

class ViewController: UIViewController {
    // MARK: - Variables

    // MARK: - IBOutlet

    // MARK: - IBAction

    // MARK: - Life cycle
    override func viewDidLoad() {
        super.viewDidLoad()

        let buttonRect = CGRect(x: 20.0, y: 160.0, width: 100.0, height: 32.0)
        let myButton = MyButton(frame: buttonRect, tintColor0: UIColor.black, tintColor1: UIColor.gray, borderColor: UIColor.orange, backColor: UIColor.white, cornerRadius: 8.0, titleString: "Hello")
        myButton.addTarget(self, action: #selector(buttonTapped(_:)), for: .touchUpInside)
        view.addSubview(myButton)
    }

    @objc func buttonTapped(_ sender: UIButton) {
        print("Hello!?")
    }
}

@MuhammadWaqasBhati made a good point asking about the frame.

I was using addSublayer to draw the path I had created onto the screen. My mistake here was that I was setting values in the draw() function and adding the CAShapeLayer with addSublayer, but a frame for the button wasn't being set.

Even though the layer that is drawn is a sublayer of the button, it appears at the coordinates and dimensions provided for the layer, without any relation to the frame of its "parent" button.

The frame of the button could be (0, 0, 0, 0) or (0, 0, 100, 100) and the image drawn in addSublayer could still be at (250, 200, 75, 80) so that the visible image would be in one spot of the screen, but the actual button is in an unrelated spot to what is visible in its sublayer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM