3

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)
    }
}
JaredH
  • 2,338
  • 1
  • 30
  • 40

3 Answers3

14

Try setting the button type to Custom, not System.

Xchord
  • 678
  • 1
  • 5
  • 20
  • Thank you so much it works! But may I ask why? Is system UIButton set to this highlighting behavior by default or something else? – Gordon Vanderbilt Aug 06 '15 at 06:02
  • 1
    Yes, that's the default behaviour of system button. – Xchord Aug 06 '15 at 06:36
  • My issue was a little different: The last selected button in a menu (3 stacked buttons in a UIView launched as a context menu) flashed every time the menu was opened. This solved my problem too. Thanks XChord. – Farhan A. Rafi Sep 08 '22 at 08:40
1

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.
App Dev Guy
  • 5,396
  • 4
  • 31
  • 54
  • 1
    The answer by @Xchord worked. But still a ton of thanks to you! Your thinking of data delivery delay is enlightening, I would probably come across problems like that in the future, better keep this solution in mind. : ) – Gordon Vanderbilt Aug 06 '15 at 06:01
0

This should fix your issue:

[UIView performWithoutAnimation:^{
  [self.myButton setTitle:text forState:UIControlStateNormal];
  [self.myButton layoutIfNeeded];
}];
Abhinav
  • 37,684
  • 43
  • 191
  • 309