I am trying to subclass UITextField as follows to implement a custom right view as a Clear button:
-(void) drawRect:(CGRect)rect { [self.layer setBackgroundColor:[[UIColor colorWithRed:20.0/255.0 green:20.0/255.0 blue:20.0/255.0 alpha:1] CGColor]]; [self.layer setCornerRadius:15.0]; UIImage *imgClear = [UIImage imageNamed:@"btnClear"]; CGSize iSize = [imgClear size]; UIButton *clearButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, iSize.width, iSize.height)]; [clearButton setImage:imgClear forState:UIControlStateNormal]; [clearButton addTarget:self action:@selector(clearText:) forControlEvents:UIControlEventTouchUpInside]; [self setRightViewMode:UITextFieldViewModeWhileEditing]; [self setRightView:clearButton]; [clearButton release]; }
but the problem is this: when the text field just becomes focus, the βclearβ becomes clear, and after I start using the keyboard, it disappears. Any ideas?
source share