3
votes

I need like to keep a UIButton highlighted after a touch event. In iOS versions < 7 I used the following action for the Touch Up Inside event:

- (IBAction)clickMe:(id)sender {
    UIButton *button = sender;
    [NSOperationQueue.mainQueue addOperationWithBlock:^{ button.highlighted = YES; }];
}

Unfortunately something has changed in iOS 7 and this code doesn't work anymore: if I tap the button, the button reverts to normal state; interestingly, if I keep the button pressed for a little longer, the button remains highlighted. Please note that the app was developed for iOS 6 so it runs in iOS 7 in compatibility mode. I'm trying to figure out a way to make the app work properly on both iOS 6 & 7 but so far I haven't found a nice solution (one workaround is to queue an event that highlights the button after a short delay but that produces an annoying flickering of the button). Any advice?

3

3 Answers

4
votes

As for the ".highlighted" property you're using, Apple documentation states: "UIControl automatically sets and clears this state automatically when a touch enters and exits during tracking and when there is a touch up."

Why not change the UIImage that your UIButton displays, depending on the "state" you want it to appear in? I would subclass UIButton, give it a state property that you can control or set, and depending on that state you can show a different image.

1
votes

try this:

[UIView animateWithDuration:1
                     animations:^{
                     }
                     completion:^(BOOL finished) {
                         button.highlighted = true;
                         button.selected = true;
                     }
];
0
votes

This might be a bit late to help you, but what I finally ended up doing to get around this issue is to set the layer background color to grey in the block for setting the button highlighted. And then when I set the highlight state to false, I set the background color back to white. For this to work, you have to use a custom button, not a rounded rect button.