У меня есть следующий код для инициализации метки:
let forgotPasswordLabel: UILabel = {
let label = UILabel()
label.text = "Forgot password?"
label.font = ApplicationScheme.instance.containerScheme.typographyScheme.subtitle1
label.textColor = ApplicationScheme.instance.containerScheme.colorScheme.onPrimaryColor
label.textAlignment = .right
label.sizeToFit()
label.accessibilityRespondsToUserInteraction = true
label.isUserInteractionEnabled = true
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
И позже я добавлю его в подпредставление следующим образом:
let passwordLabel = forgotPasswordLabel
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(onForgotPasswordClick))
passwordLabel.addGestureRecognizer(tapGesture)
self.view.addSubview(passwordLabel)
Если я сделаю код как таковой тогда он работает нормально, однако, если я помещаю код tapGesture в метку инициализации ForgotPasswordLabel, жест касания не срабатывает. Почему это так?
Код, который не работает:
let forgotPasswordLabel: UILabel = {
let label = UILabel()
label.text = "Forgot password?"
label.font = ApplicationScheme.instance.containerScheme.typographyScheme.subtitle1
label.textColor = ApplicationScheme.instance.containerScheme.colorScheme.onPrimaryColor
label.textAlignment = .right
label.sizeToFit()
label.isUserInteractionEnabled = true
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(onForgotPasswordClick))
label.addGestureRecognizer(tapGesture)
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
Это как-то связано с сборщиком мусора? Нераспознанные границы для UILabel? Или что-то еще?