Dean Lee
Dean Lee

Reputation: 821

iOS Button not work in custom view

There are two custom views - viewA and viewB.ViewB is a small view added in viewA. At first viewA is filled in the whole screen. ViewB is located in the bottom of viewA(out of the screen). When click a button in viewA, viewB bottom constant of constraints will be -100, thus viewB will display in the bottom of the screen. But there is a button in viewB did not response its selector. Here is my code:

In ViewA

let viewB: ViewB = {
    let view = ViewB(
    view.setup()
    return view
}()

viewB constraints:

    viewB.snp.makeConstraints { (make) in
        make.trailing.leading.equalTo(self)
        make.height.equalTo(100)
        make.top.equalTo(self.snp.bottom)
    }

when click button to arouse viewB

            UIView.animate(withDuration: 0.5) {
            self.snp.updateConstraints({ (make) in
                make.bottom.equalTo(-100)
            })
        }
        self.layoutIfNeeded()

In viewB:

class ViewB: UIView {

let b: UIButton = {
    let button = UIButton(type: UIButtonType.custom)
    button.addTarget(self, action: #selector(btnClicked), for: UIControlEvents.touchUpInside)
    button.backgroundColor = UIColor.green
    button.setTitle("Button", for: .normal)
    return button
}()


func btnClicked() {
   print("btnClicked")
}

func setup() {
    addSubview(b)
    b.snp.makeConstraints { (make) in
        make.leading.top.bottom.equalTo(self)
        make.width.equalTo(100)
    }

}
}

View Debugger

Upvotes: 0

Views: 1747

Answers (2)

Ajjjjjjjj
Ajjjjjjjj

Reputation: 675

i think , your view is going out of its super View or Any other view is coming on its upper layer. please see by this

superview.clipsToBounds :  YES

Upvotes: 1

Fahim
Fahim

Reputation: 3556

Your code by itself works fine. So the issue is probably something like the button being overlaid by another view which takes the touch actions. You can test this using the view debugger or provide a link to your project so one of us can take a look to see what is going on.

Upvotes: 1

Related Questions