masterBtn.addTarget(self, action: #selector(self.masterBed), for: UIControlEvents.touchUpInside)
i have used above code in subview but it doesn't fire function masterBed
.
the button within subview is not clickable
Full code:
let button = UIButton(frame: CGRect(x: 100, y: 100, width: 100, height: 50))
button.setTitleColor(.gray, for: .normal)
button.center = CGPoint(x: 380, y: 110)
button.setTitle(">", for: .normal)
button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
self.addSubview(button)
func buttonAction () {
print("button pressed")
}
I believe the height and width of the subview remains 0 because the button is not bound to any edges and the button seem to define the height of its superview. You could always check this by settings clipToBounds = true. Its always good to call the lazy if you are using self within an view.
This should solve your problem:
Slightly unsure about the NSLayoutConstraints, since i use SnapKit or anchorpoints. But i think this should be correct.