Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 496 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
UIButton in Swift is not registering touches

#1
I'm trying to create a UIButton using Swift. It compiles fine and I can see my button in the simulator, but when I click it, nothing happens. This is the code I am using:

let settings = UIButton()
settings.addTarget(self, action: "touchedSet:", forControlEvents: UIControlEvents.TouchUpInside)
settings.setTitle("Settings", forState: .Normal)
settings.frame = CGRectMake(0, 530, 150, 50)
scrollView.addSubview(settings)

In the same class, here is the function 'touchedSet':

func touchedSet(sender: UIButton!) {
println("You tapped the button")
}

I'm using the simulator as I don't have an iOS 8.0 device, could that be the problem?

Thanks!
Reply

#2
When a view like a button is placed next to another view like an image view etc. especially if if the image view is on top of another view Xcode sometimes thinks that your button is underneath that image view even though it is not. Sometimes you need to clear all the constraints and reset all constraints in order for the button to work.
Reply

#3
If you're **using Autolayout** to build your UI, **make sure to declare all your constraints** in your view and parent views.

Some times we forget to set all of them and the system complains about it, drawing your UI but not responding correctly in your touch events.

In my case, i’ve forgot to set my bottom constraint in my view so i’m here to set this comment as a reminder.
Reply

#4
In my case, I had a transparent `UIView` above the `UIButton` in the view hierarchy, which is why when I was "clicking on the `UIButton`", I was actually clicking on the transparent `UIView` on top of it.

Sending the transparent `UIView` back using `parentUIView.sendSubviewToBack(transparentUIView)` worked for me, because after doing this the `UIButton` came on top of the view hierarchy. Click on the "Debug View Hierarchy" button in Xcode to check if the `UIButton` is on top or not.
Reply

#5
I had changed the height and width constraints to 0, for some reason I needed to do this so it showed correctly on a multipurpose view for one use case, the buttons were visible but the touch stopped working.
Reply

#6
For those who are also stuck in the tutorial:

I ran in to the same problem, when I was following Apple's "Start Developing iOS Apps (Swift)". It turned out that I had overlooked these code lines in the tutorial:

override var intrinsicContentSize : CGSize {
return CGSize(width: 240, height: 44)
}

Adding them to my RatingControl fixed the problem.
Reply

#7
For anyone else doing manual view layout running into this issue, you might have defined your subview like such:

let settingsButton: UIButton = {
let button = UIButton(type: .system)
// do some more setup
button.addTarget(self, selector: #selector(openSettings), for: .touchUpInside)
return button
}()

The error lies with the fact that you are adding a target and passing `self` before it is actually available.

**This can be fixed in two ways**

1. Making the variable `lazy` and still adding the target in the initialization block:

lazy var button: UIButton = {
let button = UIButton(type: .system)
// since this variable is lazy, we are guaranteed that self is available
button.addTarget(self, selector: #selector(openSettings), for: .touchUpInside)
return button
}()
2. Adding the target after your parent view has been initialized:

init() {
self.settingsButton = .init(type: .system)
super.init(frame: .zero)
self.settingsButton.addTarget(self, selector: #selector(openSettings), for: .touchUpInside)
}

Reply

#8
Old question, but I also found myself stuck with an unresponding button. I simply changed my initialisation from

```
let button = UIButton(frame: .zero)
```

to

```
let button = UIButton(type: .system)
```
Reply

#9
I had the same issue. The problem was the view had top constraint, but not left/right and height constraints. So, the view was shrinking to 1x1 and it was not passing the touch event to children.

By adding more constraints, the children now getting the touch event and working ...


let guide = view.safeAreaLayoutGuide
viewHeader.topAnchor.constraint(equalTo: guide.topAnchor).isActive = true
viewHeader.rightAnchor.constraint(equalTo: guide.rightAnchor).isActive = true
viewHeader.leftAnchor.constraint(equalTo: guide.leftAnchor).isActive = true
let heightConstraint = NSLayoutConstraint(item: viewHeader,
attribute: NSLayoutConstraint.Attribute.height,
relatedBy: NSLayoutConstraint.Relation.equal,
toItem: nil,
attribute: NSLayoutConstraint.Attribute.notAnAttribute,
multiplier: 1,
constant: 44)
viewHeader.addConstraints([heightConstraint])


Reply

#10
Interestingly enough, I just ran into the same issue on the very latest versions of iOS/Xcode (v12.4 / v10.3). Turns out the issue for me was a LayoutConstraint! No joke. I had a leading label with the uiSwitch to the right, and found that I needed to change the constraints between the two such that it wasn't a fixed constant value of 8 points (Label-8-Switch-0-|).

As soon as I changed this to a ">=" the Switch was able to change states between on/off. Laughably, it's almost like it wasn't able to change because it needs "room" (that apparently varies) to make the change with.

Not sure, but file it away as one of those "hummmm?" items in your memory.

One other item (that really shouldn't matter, frankly) is the valueChanged and tappedUpInside were both not firing (true on both an actual handset and on the simulators). Also, they were hooked up through a storyboard. But, this shouldn't matter as far as I know.
Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through