Gordon Vanderbilt
Gordon Vanderbilt

Reputation: 369

How to stop UIButton from highlighting/flash each time its title is reset?

I'm using a UIButton as a label to display some number in its title. Every time the function is called to reset the title of this button, it flashes. It does not affect the functionality but hurts user experience. I'm wondering if there is a way to stop UIButton from this highlighting behavior?

Thanks in advance!

Edit: Following are the code where I'm simply calling a delegate method updateDigits to refresh the button's title.

class ViewController: UIViewController, UIPopoverPresentationControllerDelegate, DigitsEntryViewDelegate {

    @IBOutlet weak var tipField1: UIButton!
    @IBOutlet weak var tipField2: UIButton!

    var buttonsArray = [UIButton]()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.tipField1.tag = 0
        self.tipField2.tag = 1

        self.tipField1.addTarget(self, action: "inputFieldClicked:", forControlEvents: UIControlEvents.TouchUpInside)
        self.tipField2.addTarget(self, action: "inputFieldClicked:", forControlEvents: UIControlEvents.TouchUpInside)

        self.buttonsArray.append(self.tipField1)
        self.buttonsArray.append(self.tipField2)
    }


    func inputFieldClicked(sender: UIButton) {
        let anchor: UIView = sender

        let viewControllerForPopover = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("DigitEnter") as! DigitsEntryViewController

        // set popover API values
        viewControllerForPopover.currentDigitsString = sender.currentTitle
        viewControllerForPopover.tagNumber = sender.tag

        viewControllerForPopover.delegate = self
        viewControllerForPopover.preferredContentSize = CGSizeMake(240, 320)
        let popover = UIPopoverController(contentViewController:viewControllerForPopover)
        popover.presentPopoverFromRect(anchor.frame, inView: anchor.superview!, permittedArrowDirections: UIPopoverArrowDirection.Down, animated: false)
    }

    func updateDigits(returnedDigits: String, tagNumber: Int) {
        self.buttonsArray[tagNumber].setTitle(returnedDigits, forState: UIControlState.Normal)
    }
}

Upvotes: 3

Views: 2268

Answers (3)

App Dev Guy
App Dev Guy

Reputation: 5536

I could be mistaken, but the flashing looks like it may be caused by a data delivery delay; you are attempting to change the title while the new title is still being retrieved, hence the flash.

I would:

  1. Retrieve the data and store it in a temporary, but purpose created string.
  2. After the delivery of data is made, call a method to change the text of the button to that of the stored string.

Upvotes: 1

Xchord
Xchord

Reputation: 688

Try setting the button type to Custom, not System.

Upvotes: 14

Abhinav
Abhinav

Reputation: 38142

This should fix your issue:

[UIView performWithoutAnimation:^{
  [self.myButton setTitle:text forState:UIControlStateNormal];
  [self.myButton layoutIfNeeded];
}];

Upvotes: 0

Related Questions