7

This seems to be a bit of a weird issue. I have a UIButton in an iterated tableViewCell in a tableView thats been placed in a regular ViewController. For some reason, it only looks clicked (highlights from its default blue color to the lighter blue briefly and then back to the blue) on long press. It does whatever action I assign it (just started with a normal old print statement) on the regular click, but just doesn't look clicked. Anybody know how to fix this? Button in its normal state: Button in its normal state

Button after being clicked:

Button after being clicked

Button after being long pressed (sorry, had to take a picture with phone for this)

enter image description here

Edit: This is different from the answers in the iOS7 question because I am looking for an iOS 10 swift solution, not objective-c. Nor iOS7

Runeaway3
  • 1,439
  • 1
  • 17
  • 43
  • 1
    Could you add screenshots or a gif to show what's going on? I can't quite tell from your description what's going on. – Jadar Dec 29 '16 at 02:45
  • @Jadar , I have posted the relevant pictures. The normal state and after a single click button look identical. Only after long press does it become highlighted (and become lighter) and then it reverts back to the normal state color after letting go – Runeaway3 Dec 29 '16 at 02:57
  • Possible duplicate of [UIButton not showing highlight on tap in iOS7](http://stackoverflow.com/questions/19256996/uibutton-not-showing-highlight-on-tap-in-ios7) – Jadar Dec 29 '16 at 03:18
  • @Jadar I took a look at that question, but I could not find a solution. This is iOS10, and I am programming in swift, not objective-c – Runeaway3 Dec 29 '16 at 03:22
  • That answer is convertible to Swift and it does answer the question. The only issue is that is uses private classes and hacks the table view into submission which is never a good idea to do. – Jadar Dec 29 '16 at 03:29

1 Answers1

8

This is normal behavior. It's due to UIScrollView's delaysContentTouches property. Per Apple's docs:

If the value of this property is YES, the scroll view delays handling the touch-down gesture until it can determine if scrolling is the intent. If the value is NO , the scroll view immediately calls touchesShouldBegin:withEvent:inContentView:.

EDIT: This answer gives the the below solution. I've tested it and it does work. However, as it's doing casting magic, I wouldn't recommend you actually do this because it will inevitably break when Apple decides to change their view hierarchy behind-the-scenes.

tableView.delaysContentTouches = false
for case let x as UIScrollView in tableView.subviews {
    x.delaysContentTouches = false
}

OLD ANSWER: This answer gives a solution, but it's really a hack and might cause your app to get rejected by Apple due to its use of private classes. Since the behavior is really a feature, I'd recommend you to leave it as it is.

Community
  • 1
  • 1
Jadar
  • 1,643
  • 1
  • 13
  • 25
  • 1
    I'm not sure if that makes sense; the buttons actions is called right away, its only the highlight display that does not happen. If its essentially waiting for the app to determine if its a scroll or not, why is the action called right away? Also, in a `tableViewController` (this scenario for the question is a `tableView` placed into a `ViewController`), this does not happen. There is no delay, and a button can be seen as pressed immediately. – Runeaway3 Dec 29 '16 at 03:37
  • It is curious that the action is handled but the UI is delayed. However when I tested, I did it with a UITableViewController and I did see the delay. – Jadar Dec 29 '16 at 14:50
  • So I think it is to do with the different UIScrollViews employed by the UITableView. No matter the way it's created. – Jadar Dec 29 '16 at 17:32
  • ok I would like to try this. Where does this block of code go into? `cellForRowAt IndexPath`? – Runeaway3 Dec 29 '16 at 23:31
  • nevermind I got it. But what happens when they change their view hierarchy? Would there just be no solution at all? – Runeaway3 Dec 29 '16 at 23:39
  • viewDidLoad works. If they change it then we'll need to figure out what changed and how to accommodate for it. But it's be broken on that iOS version until you updated your app. – Jadar Dec 30 '16 at 03:09