Keep iPhone UIButton Highlighted

风格不统一 提交于 2020-01-19 05:37:45

问题


I have the following code snippets:

@interface Foo: UIViewController {
  ...
  UIButton *myButton;
  ...
}

@implementation Foo

- (void) viewDidLoad {
  ...
  myButton.highlighted = YES;
  ...
}

When I run the app, the button is highlighted in blue (default behavior). It works as I expected.

But after pressing the button once, the button is no longer highlighted.

Then, I created an IBAction highlightButton to handle Touch Up Inside event where I explicitly call myButton.highlighted = Yes;. Unfortunately, the button highlight still does not stay.

How can I keep it highlighted in blue even after being pressed?


回答1:


The solution is to do [button setHighlighted:YES] in the next runloop:

- (void)highlightButton:(UIButton *)b { 
    [b setHighlighted:YES];
}

 - (IBAction)onTouchup:(UIButton *)sender {
    [self performSelector:@selector(highlightButton:) withObject:sender afterDelay:0.0];
}



回答2:


The simplest code is here.

dispatch_async(dispatch_get_main_queue(), ^{
    [button setHighlighted:YES];
});



回答3:


An alternate way to run this is by sending a block to the main operation queue:

-(void)onTouchup:(UIButton*) button
{
    [NSOperationQueue.mainQueue addOperationWithBlock:^{ button.highlighted = YES; }];
}


来源:https://stackoverflow.com/questions/2290534/keep-iphone-uibutton-highlighted

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!