简体   繁体   中英

Why is my UIButton target action not being called?

I am using Google Maps API, and I have created a UIButton to be placed on top of the map view. Inside that UIButton, I have inserted a UIImageView. For some reason, after I add a target action to the UIButton, the selector method is not being called. Here is an example of what the elements look like when the app is built:

在设备上运行的应用程序的屏幕截图

Here is my selector method:

@objc func goToListView() {
    print("Go to list view!")
}

Here is my UIButton code:

func configureReturnToListViewItem() {
    view.addSubview(returnToListViewItem)
    returnToListViewItem.backgroundColor = .white
    returnToListViewItem.layer.cornerRadius = 25
    returnToListViewItem.clipsToBounds = true
    returnToListViewItem.isUserInteractionEnabled = true
    returnToListViewItem.translatesAutoresizingMaskIntoConstraints = false
    returnToListViewItem.heightAnchor.constraint(equalToConstant: 50).isActive = true
    returnToListViewItem.widthAnchor.constraint(equalToConstant: 50).isActive = true
    returnToListViewItem.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 25).isActive = true
    returnToListViewItem.topAnchor.constraint(equalTo: view.topAnchor, constant: 50).isActive = true
    returnToListViewItem.addTarget(self, action: #selector(goToListView), for: .touchUpInside)
}

And here is the code for the image inside the UIButton, just in case:

func configureReturnToListViewImage() {
    returnToListViewItem.addSubview(returnToListViewImage)
    let image = UIImage(named: "list")
    returnToListViewImage.image = image
    returnToListViewImage.contentMode = .scaleAspectFill
    returnToListViewImage.clipsToBounds = true
    returnToListViewImage.layer.masksToBounds = true
    returnToListViewImage.translatesAutoresizingMaskIntoConstraints = false
    returnToListViewImage.heightAnchor.constraint(equalToConstant: 20).isActive = true
    returnToListViewImage.widthAnchor.constraint(equalToConstant: 20).isActive = true
    returnToListViewImage.centerXAnchor.constraint(equalTo: returnToListViewItem.centerXAnchor).isActive = true
    returnToListViewImage.centerYAnchor.constraint(equalTo: returnToListViewItem.centerYAnchor).isActive = true
}

I have tried tap gestures, as well as setting isUserInteractionEnabled to true for all elements within the view controller. I would love some guidance!

Edit: My problem was due to an invisible navigation bar, caused by earlier configuration. In most cases, the other answer would be correct - so please use those answers as well if they apply to your situation.

Seems returnToListViewImage is covering up the returnToListViewItem . Try without the returnToListViewImage to see if it's getting the target triggered. If yes you can add a tap gesture on returnToListViewImage and set the target as goToListView don't forget to set the isUserIteractionEnabled is true for returnToListViewImage and it would work the same.

Better approach: You could directly set the image to the UIButton type returnToListViewItem , like this and this would require only one line instead of the whole function you've given.

returnToListViewItem.setImage(UIImage(named: "list"), for: .normal)

I found the answer to my question by using the view debugger after running my application. In my specific case, my invisible navigation bar was covering the buttons, which prevented any interaction with anything underneath the navigation bar. All I had to do was add this line of code to my viewDidLoad() function:

self.navigationController?.setNavigationBarHidden(true, animated: false)

The view debugger is a lifesaver!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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