繁体   English   中英

自定义UIScrollView类中的UIButton不响应触摸

[英]UIButton in custom UIScrollView class not responding to touches

我有一个如下的视图结构;

-->Custom Scroll View
    -->Container View
       -->View
          -->Button

我的问题是我向UIButton添加了一个目标,但是未调用touch函数。 我试图在自定义视图类或视图控制器中添加目标。 但是,两者都不起作用。

我检查了什么;

  • 我检查是否所有视图的isUserInteractionEnabled都设置为true,并且所有视图都为true。
  • 我还检查了按钮是否具有框架(可以看到一些有关UIButton的说法,但您不能触摸它,因为它没有框架)。

我检查了堆栈溢出中的每个答案,其中大多数都讨论了isUserInteraction和层次结构,我认为它们在我的情况下都是正确的。

自定义视图调用时,UIButton不可点击

自定义UIView添加目标未调用

我如何创建自定义视图并在其中添加按钮。

class TyreChangeScrollView: UIScrollView {

//E-Mail
let emailTitleLbl = BaseLabel()
let emailBgView = UIView()
let emailLbl = BaseLabel()
public let emailInfoBtn = UIButton()

let contentView: UIView = {
    let v = UIView()
    v.translatesAutoresizingMaskIntoConstraints = false
    return v
}()

var shouldSetupConstraints = true

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

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

func updateUI(){
    addSubview(contentView)

    emailTitleLbl.translatesAutoresizingMaskIntoConstraints = false
    emailTitleLbl.text =  "E-POSTA ADRESİ"
    emailTitleLbl.textAlignment = .left
    contentView.addSubview(emailTitleLbl)

    emailLbl.translatesAutoresizingMaskIntoConstraints = false
    emailLbl.text =  "hello@hello.com"
    emailLbl.textAlignment = .left
    emailInfoBtn.setImage(UIImage(named: "info"), for: .normal)
    emailInfoBtn.isUserInteractionEnabled = true
    emailBgView.backgroundColor = Color.Common.fieldBg
    emailBgView.addSubview(emailLbl)
    emailBgView.addSubview(emailInfoBtn)
    contentView.addSubview(emailBgView)
}
override func updateConstraints() {
    if(shouldSetupConstraints) {
        // AutoLayout constraints
        contentView.translatesAutoresizingMaskIntoConstraints = false
        // constrain the scroll view to 8-pts on each side
        contentView.anchor(self.topAnchor, left: self.leftAnchor, bottom: self.bottomAnchor, right: self.rightAnchor, topConstant: 0, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 0)
        contentView.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: 1.0).isActive = true

        emailTitleLbl.anchor(phoneLbl.bottomAnchor, left: contentView.leftAnchor, bottom: nil, right: contentView.rightAnchor, topConstant: 10, leftConstant: 20, bottomConstant: 0, rightConstant: 16, widthConstant: 0, heightConstant: 0)
        emailLbl.anchor(emailBgView.topAnchor, left: emailBgView.leftAnchor, bottom: emailBgView.bottomAnchor, right: nil, topConstant: 0, leftConstant: 15, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 0)
        emailInfoBtn.anchor(emailBgView.topAnchor, left: nil, bottom: emailBgView.bottomAnchor, right: emailBgView.rightAnchor, topConstant: 0, leftConstant: 0, bottomConstant: 0, rightConstant: 10, widthConstant: 0, heightConstant: 0)
        emailBgView.anchor(emailTitleLbl.bottomAnchor, left: contentView.leftAnchor, bottom: nil, right: contentView.rightAnchor, topConstant: 10, leftConstant: 20, bottomConstant: 0, rightConstant: 16, widthConstant: 0, heightConstant: 50)

        shouldSetupConstraints = false
    }
    super.updateConstraints()
}
}

我如何声明自定义类并将目标添加到UIButton。

class TyreChangeViewController: BaseViewController{

let scrollView = TyreChangeScrollView()

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    designUI()
    scrollView.emailInfoBtn.addTarget(self, action: #selector(emailBtnTapped(_:)), for: UIControlEvents.touchUpInside)
    scrollView.phoneInfoBtn.addTarget(self, action: #selector(phoneBtnTapped(_:)), for: UIControlEvents.touchUpInside)

}
override func viewWillAppear(_ animated: Bool) {

    scrollView.anchor(topLayoutGuide.bottomAnchor, left: view.leftAnchor, bottom: bottomLayoutGuide.topAnchor, right: view.rightAnchor, topConstant: 10, leftConstant: 10, bottomConstant: 10, rightConstant: 10, widthConstant: 0, heightConstant: 0)
}
func designUI(){

    view.backgroundColor = Color.Common.screenBgColor
    scrollView.backgroundColor = Color.Common.screenBgWhite
    view.addSubview(scrollView)

}
@objc func emailBtnTapped(_ sender: UIButton){
    print("hello")
}}

编辑:如果我在内容视图中添加了高度限制,则按钮可以正常工作,但现在无法滚动。

contentView.heightAnchor.constraint(equalTo: self.heightAnchor, multiplier: 1.0).isActive = true

添加约束后,刷新布局是必须的

override func viewWillAppear(_ animated: Bool) {

    scrollView.anchor(topLayoutGuide.bottomAnchor, left: view.leftAnchor, bottom: bottomLayoutGuide.topAnchor, right: view.rightAnchor, topConstant: 10, leftConstant: 10, bottomConstant: 10, rightConstant: 10, widthConstant: 0, heightConstant: 0)

    self.view.layoutIfNeeded()
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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