UITextField rightViewMode odd behaviour

前端 未结 3 960
夕颜
夕颜 2020-12-30 08:13

I\'m adding a custom clear button (UIButton) to a UITextField as the rightView, however I\'ve found there\'s some weird behaviour on the viewMode. It doesn\'t seem to displa

相关标签:
3条回答
  • 2020-12-30 08:54

    Simple code for solve this problem

    - (void)textFieldDidBeginEditing:(UITextField *)textField
    {
        textField.rightViewMode=UITextFieldViewModeAlways;
    }
    
    - (BOOL)textFieldShouldEndEditing:(UITextField *)textField
    {
         textField.rightViewMode=UITextFieldViewModeNever;
        return YES;
    }
    
    0 讨论(0)
  • 2020-12-30 08:56

    I recently ran into the same problem and ended up setting right view mode to UITextFieldViewModeAlways and manually showing/hiding that button when it's needed (made proxy delegate which monitored text field state, set button's visibility and passed messages to actual delegate).

    0 讨论(0)
  • 2020-12-30 09:13

    This fixes the bug :

    - (BOOL)becomeFirstResponder
    {
        BOOL ret = YES ;
    
        ret = [super becomeFirstResponder] ;
    
        if( ret && ( _setupClearButtonMode == UITextFieldViewModeWhileEditing ) )
            self.rightViewMode = UITextFieldViewModeAlways ;
    
        return ret ;
    }
    
    - (BOOL)resignFirstResponder
    {
        BOOL ret = YES ;
    
        ret = [super resignFirstResponder] ;
    
        if( ret && ( _setupClearButtonMode == UITextFieldViewModeWhileEditing ) )
            self.rightViewMode = UITextFieldViewModeWhileEditing ;
    
        return ret ;
    }
    

    In your subclass of UITextField with the var _setupClearButtonMode set on init.

    0 讨论(0)
提交回复
热议问题