Emre Önder
Emre Önder

Reputation: 2537

UIButton in custom UIScrollView class not responding to touches

I have a view structure like below;

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

My problem is that I'm adding a target to UIButton but the touch function is not called. I tried to add the target both in custom view class or view controller. However, neither worked.

What I checked;

I checked probably every answer in stack overflow and most of them talk about isUserInteraction and hierarchy and I think they are all correct in my situation.

UIButton not clickable when custom view called

Custom UIView add target not called

How I create custom view and add button inside of it.

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 =  "[email protected]"
    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()
}
}

How I declare custom class and add target to 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")
}}

EDIT: If I add a height constraint to the content view, buttons are working but now it isn't scrollable.

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

Upvotes: 0

Views: 1171

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100503

After adding a constraint refreshing layout is a must

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()
 }

Upvotes: 1

Related Questions