Cannot Tap Title in Custom Button
I Create custom button apple Sign up apple, error in here code:
private func setupSignUpWithAppleButton() {
let signUpWithAppleButton: SignInAppleButton = SignInAppleButton()
signUpWithAppleButton.addTarget(self, action: #selector(didTapSignUpWithAppleButton(_:)), for: .touchUpInside)
subView.addSubview(signUpWithAppleButton)
signUpWithAppleButton.fillInSuperview()
}
This fixing code, with change addTarget to tap Gesture
private func setupSignUpWithAppleButton() {
let signUpWithAppleButton: SignInAppleButton = SignInAppleButton()
subView.addSubview(signUpWithAppleButton)
signUpWithAppleButton.fillInSuperview()
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(didTapSignUpWithAppleButton))
signUpWithAppleButton.addGestureRecognizer(tapGesture)
}
this full custom UIButton
public final class SignInAppleButton: UIButton {
private lazy var containerView: UIView = {
let view: UIView = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
view.backgroundColor = .clear
return view
}()
private lazy var appleLogoImageView: UIImageView = {
let imageView: UIImageView = UIImageView()
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.image = UIImage(named: "appleLogo")?.withRenderingMode(.alwaysTemplate)
imageView.tintColor = .white
imageView.contentMode = .scaleAspectFit
return imageView
}()
private lazy var textLabel: UILabel = {
let label: UILabel = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.font = .systemFont(ofSize: 16, weight: .medium)
label.textColor = .white
return label
}()
public init() {
super.init(frame: .zero)
setupViews()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func setupViews() {
backgroundColor = .black
layer.cornerRadius = 6.0
addSubview(containerView)
containerView.addSubview(appleLogoImageView)
containerView.addSubview(textLabel)
NSLayoutConstraint.activate([
containerView.topAnchor.constraint(equalTo: topAnchor),
containerView.bottomAnchor.constraint(equalTo: bottomAnchor),
containerView.centerXAnchor.constraint(equalTo: centerXAnchor),
appleLogoImageView.topAnchor.constraint(equalTo: containerView.topAnchor),
appleLogoImageView.leftAnchor.constraint(equalTo: containerView.leftAnchor),
appleLogoImageView.bottomAnchor.constraint(equalTo: containerView.bottomAnchor),
appleLogoImageView.widthAnchor.constraint(equalToConstant: 12),
textLabel.topAnchor.constraint(equalTo: containerView.topAnchor),
textLabel.leftAnchor.constraint(equalTo: appleLogoImageView.rightAnchor, constant: 6),
textLabel.rightAnchor.constraint(equalTo: containerView.rightAnchor),
textLabel.bottomAnchor.constraint(equalTo: containerView.bottomAnchor)
])
textLabel.text = "Sign up with Apple"
layoutIfNeeded()
}
}