2017-04-18 50 views
1

當我使用setTitle: forState:方法設置我的按鈕的標題時,有淡出和淡入效果。當我設置按鈕標題時,有淡入淡出效果

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

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


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

    }else { 

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

    } 

} 

如何解決?

回答

2

嘗試使用它。

- (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]; 
    }]; 

    } 
} 
0

當您設置按鈕標題時,如果不想要效果,您可以先設置titleLable.text

試試這個:

- (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]; 

    } 

} 
0

試試這個它是工作在我的代碼:

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