I have a custom cell with a few UIButtons in it. I have created Target Actions like so on the buttons:
[cell.customLocationButton addTarget:self action:@selector(customLocationButtonTap:) forControlEvents:UIControlEventTouchUpInside];
The actions work great though I don't see the default iOS fade animation on the UIButtons? Is there something I need to enable to get those too - I thought they were standard when using a UIButton from IB?
Had the same problem. I could get the button to change color on highlight with setTitleColor:forState:
, but it didn't fade from highlight to normal state like the other system buttons.
Turns out that's because my button was of type UIButtonTypeCustom
. Switching it to UIButtonTypeSystem
solved it for me. Please note I'm running this on iOS9.
Here's a snippet (Swift) that assumes self
is a UIView
or subclass:
let button = UIButton(type: .System)
button.setTitle("Title", forState: .Normal)
button.sizeToFit() // Sizes the button frame based on the title.
addSubview(button)
I've been searching all over the internet and finally I've found a solution from a post on StackOverFlow check it out here.
For Swift 4 You can add this UIButton extension and it will work fine.
extension UIButton {
override open func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
isHighlighted = true
super.touchesBegan(touches, with: event)
}
override open func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
isHighlighted = false
super.touchesEnded(touches, with: event)
}
override open func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
isHighlighted = false
super.touchesCancelled(touches, with: event)
}
}
In your XIB inside attribute inspector select your button and then inside button type you will get follow option you can select and change your button look and feel accordingly:-
![](https://www.manongdao.com/static/images/pcload.jpg)
Another thing you can do is:
Swift:
button.showsTouchWhenHighlighted = true
button.setTitleColor(UIColor.blueColor(), forState: .Normal)
button.setTitleColor(UIColor.lightGrayColor(), forState: .Selected)