How to prevent UIButton from flashing when updating the title

前端 未结 9 1024
旧巷少年郎
旧巷少年郎 2021-01-30 04:41

When I call setTitle on a UIButton, the button flashes in iOS 7. I tried setting myButton.highlighted = NO, but that didn\'t stop the button from flashing.

[myBu         


        
9条回答
  •  囚心锁ツ
    2021-01-30 05:45

    A better approach than [UIView setAnimationsEnabled:NO] which may affect other animations is to only disable the specific title animation.

    Objective-C:

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

    Swift:

    UIView.performWithoutAnimation { 
        myButton.setTitle(text, for: .normal)
        myButton.layoutIfNeeded()
    }
    

提交回复
热议问题