Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIbutton only looks clicked (highlighted) on longPress?

Tags:

xcode

uibutton

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

like image 848
Runeaway3 Avatar asked Dec 29 '16 00:12

Runeaway3


1 Answers

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.

like image 105
Jadar Avatar answered Oct 09 '22 15:10

Jadar