15

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?

4
  • 1
    show your customLocationButton initialization part
    – manujmv
    Dec 10, 2013 at 8:27
  • I don't initialize. I din't think you needed to initialize when using IB?
    – wholly_cow
    Dec 10, 2013 at 8:32
  • 1
    Just Use the type of UIButtonTypeSystem for your customLocationButton.. Hope that the customLocationButton as of Custom type.
    – Kumar KL
    Dec 10, 2013 at 8:40
  • the button type is a read-only property. In IB it is already set to System
    – wholly_cow
    Dec 10, 2013 at 9:38

6 Answers 6

15

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)
7

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)
    }

}
1
  • That's genius, with an override of isHighlighted it is way faster than doing animation on the touches methods ! Mar 19, 2019 at 10:17
4

I found this solution:

setTitleColor(UIColor.init(white: 1, alpha: 0.3), for: .highlighted)

It's really simple and works perfectly.

1
  • 1
    Simple, fast and accurate. Works perfectly, thank you!
    – AdPiscinam
    Apr 4, 2022 at 9:17
1

you probably initialized the button like this:

let button = UIButton.init(frame: CGRect.init(x: 0, y: 0, width: <width>, height: <height>)

try to do it like this:

let button = UIButton.init(type: .system)
button.frame = CGRect.init(x: 0, y: 0, width: <width>, height: <height>)

The "fade" animation on tap, comes with system style, after init the button you can customize it.

0

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:-

enter image description here

2
  • Yes in system you need to set but if you want some other animations then you need to use CABasicAnimation Dec 10, 2013 at 9:55
  • If I drag out a button and place it directly in the default view of a VC I get a basic fade animation on tap. This is without setting up any custom animation. When I'm using a button in a custom cell - do I then need to add the CABasicAnimation layer manually?
    – wholly_cow
    Dec 10, 2013 at 9:59
0

Another thing you can do is:

Swift:

button.showsTouchWhenHighlighted = true
button.setTitleColor(UIColor.blueColor(), forState: .Normal)
button.setTitleColor(UIColor.lightGrayColor(), forState: .Selected)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.