简体   繁体   中英

UIButton touch not registering (Swift)

I've created a button, but for some reason, the touch isn't recognizing:

override func viewDidLoad() {
        super.viewDidLoad()

        var settingsButton = UIButton.buttonWithType(UIButtonType.System) as UIButton
        settingsButton.frame = CGRectMake(self.view.frame.size.width - 50, 35, 35, 35)
        settingsButton.setImage(UIImage(named: "settings-button"), forState: UIControlState.Normal)
        settingsButton.addTarget(self, action: Selector("settingsButtonPressed"), forControlEvents: UIControlEvents.TouchUpInside)

        self.view.addSubview(settingsButton)
}

func settingsButtonPressed(){
        println("Settings selected")
}

I'm not getting any errors, it's just not registering when I touch the button. I've tried making a button of type UIButtonType.Custom as well, and I've tried the selector as

Selector("settingsButtonPressed")
Selector("settingsButtonPressed:") //I changed the function to settingsButtonPressed(sender: UIButton)
"settingsButtonPressed"
"settingsButtonPressed:" //I changed the function to settingsButtonPressed(sender: UIButton)

There are many ways to pick up a problem when a subview/button is not clickable, I will mention the possible ways to fix it below.

  1. You should make sure isUserInteractionEnabled = true

  2. Make sure isEnabled = true

  3. You can use the Xcode 3d view hierarchy tool. You do this on Xcode by first running your app, and navigating to the screen where touches don't work. Click on the button highlighted in blue as per my example below.

XcodeGraphicalTool

After you do the above, your app will be paused and a graphical hierarchy of your app will appear, when it does zoom in and rotate to see what View is blocking your button/View. check image below for reference

在此处输入图片说明

  1. Lastly a subview might not be clickable because its outside the bounds of its super view so you need to make sure all subviews are in your superview bounds.

Hope this helps someone.

One common cause of this problem that I don't see mentioned here is that the button's userInteractionEnabled value must be true.

A much more unique cause is when the parent view is the problem. In my case I had an image view within a container view, and added a button as a subview of the image view so it would display on top and allow users to tap the button to change the image.

Nothing I did would get the button action to work, until I tried making the button a subview of the container view, and it suddenly started working. In my case it appears the UIImageView wasn't allowing subviews to get the tap events.

Chances are a link is broken between the UI element and the view controller. Try recreating the link between the ui element and the view controller.

On your view controller look over to the right hand bar where you set breakpoints and look for a little circle on the line where your button is linked. If it's filled you're linked. If the middle is hollow it's not linked.

Use this-

self.settingsButton.addTarget(self, action: Selector("settingsButtonPressed:"), forControlEvents: UIControlEvents.TouchUpInside)

and

func settingsButtonPressed(sender:UIButton!){
        println("Settings selected")
}

Look at the answer from How to create a button programmatically?

provided by Anil . It works I checked.

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