How can I move the clear button in a UITextField?

前端 未结 7 1587
既然无缘
既然无缘 2021-02-13 13:14

For some reason, when I add a UITextfield as a subview of the contentview of a tablecell, the clearbutton does not align with the text typed in the field, and appears a bit unde

7条回答
  •  情书的邮戳
    2021-02-13 13:49

    I had subclassed the UITextField and override the function clearButtonRectForBounds:.

    .h

    #import 
    
    @interface TVUITextFieldWithClearButton : UITextField
    
    @end
    

    .m

    #import "TVUITextFieldWithClearButton.h"
    
    @implementation TVUITextFieldWithClearButton
    
    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            // Initialization code
        }
        return self;
    }
    
    - (void)awakeFromNib
    {
        self.clearButtonMode = UITextFieldViewModeWhileEditing;
    }
    
    
    - (CGRect)clearButtonRectForBounds:(CGRect)bounds
    {
        return CGRectMake(bounds.size.width/2-20 , bounds.origin.y-3, bounds.size.width, bounds.size.height);
    }
    
    @end
    

提交回复
热议问题