I've made plenty of buttons and I can't figure out why this one won't work.
My hierarchy is set up like this:
View -> Scroll View -> Subview -> Button
There's a lot of other items in this subview and every one of them works when touched. In the interface builder I can see that the "Touch Up Inside" event is bound to my method
- (IBAction)favoritesButtonDepressed:(id)sender
However, when I click the button, that method never executes and my breakpoint is subsequently never hit.
I noticed that even when I hold down my press on the button, the label doesn't change color (even though shows touch on highlight is enabled), but yet my other button on the view does.
User Interaction is enabled on the button as well as everything up the hierarchy.
In order for my items in the sub view to receive touch events, I made my scroll view look like the 4th answer in this post.
You can use the following code. class ViewController: UIViewController { var gameTimer: Timer! @IBAction func btnClick(_ sender: Any) { let button = UIButton() if (button. isSelected) { print(" Not Selected"); } else { print(" Selected"); } } override func viewDidLoad() { super.
make your button Type - "Custom" and Uncheck - Highlighted Adjust image and you are done.
Aha, I got it. It turns out the subview which contained the button (along with the other controls) had a height of 740 and the button was at a Y of 781. Moving the button up fixed the issue. Since everything was displaying correctly, I didn't think to check the subview's height.
Make sure that your button is placed inside superView's bounds,
If a button is placed outside superView's bounds, the button will not clickable,
because of the implement of the hitTest
function
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With