简体   繁体   中英

When I set button title, there is fade-off and fade-out effect

When I use setTitle: forState: method to set my button's title, there have the fade-out and fade-in effect.

- (void)respondsToTextField:(UITextField *)sender {

    if ([sender.text isEqualToString:@""]) {


        [_cancleOrConfirmButton setTitle:@"Cancel" forState:UIControlStateNormal];

    }else {

        [_cancleOrConfirmButton setTitle:@"Search" forState:UIControlStateNormal];

    }

}

How to resolve it?

Try with it.

- (void)respondsToTextField:(UITextField *)sender {

    if ([sender.text isEqualToString:@""]) {

       [UIView performWithoutAnimation:^{
           [_cancleOrConfirmButton setTitle:@"Cancel" forState:UIControlStateNormal];
          [_cancleOrConfirmButton layoutIfNeeded];
    }]; 

    }else {

    [UIView performWithoutAnimation:^{
       [_cancleOrConfirmButton setTitle:@"Search" forState:UIControlStateNormal];
       [_cancleOrConfirmButton layoutIfNeeded];
    }]; 

    }
}

When You set the button title, if do not want the effect, you can set the titleLable.text first.

Try this:

- (void)respondsToTextField:(UITextField *)sender {

    if ([sender.text isEqualToString:@""]) {

        _cancleOrConfirmButton.titleLabel.text = @"Cancel";
        [_cancleOrConfirmButton setTitle:@"Cancel" forState:UIControlStateNormal];

    }else {

        _cancleOrConfirmButton.titleLabel.text = @"Search";
        [_cancleOrConfirmButton setTitle:@"Search" forState:UIControlStateNormal];

    }

}

Try this it is working in my code:

[UIView setAnimationsEnabled:NO];
[_cancleOrConfirmButton setTitle:@"Cancel" forState:UIControlStateNormal];
[UIView setAnimationsEnabled:YES];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM