UITextField rightView "WhileEditing" Problem

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?

+6
source share
6 answers

I am also facing the same problem. I assume this is an iOS bug, however I tried to fix this problem by doing an implementation, and it works fine for me. Hope this helps you.

 - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString { ... [self addTarget:self action:@selector(onEditing:) forControlEvents: UIControlEventEditingChanged] ... } -(void) onEditing:(id)sender { if(![self.text isEqualToString:@""]){ self.rightViewMode = UITextFieldViewModeAlways; }else{ self.rightViewMode = UITextFieldViewModeNever; } } - (BOOL)becomeFirstResponder{ BOOL ret = YES ; ret = [super becomeFirstResponder] ; if( ret & ![self.text isEqualToString:@""]){ self.rightViewMode = UITextFieldViewModeAlways; }else{ self.rightViewMode = UITextFieldViewModeNever; } return ret ; } - (BOOL)resignFirstResponder { BOOL ret = YES ; ret = [super resignFirstResponder] ; if( ret ) self.rightViewMode = UITextFieldViewModeNever; return ret ; } - (void) clearText:(id)sender { self.text = @""; self.rightViewMode = UITextFieldViewModeNever; } 
+5
source

You use: [self setRightViewMode:UITextFieldViewModeAlways];

+3
source

Subclass UITextField and Override -layoutSubviews.

 - (void)layoutSubviews { [super layoutSubviews]; // HACK: There is an iOS bug where the right view is not displayed when there is text in the text field. Also, iOS adds and removes the rightView. This code adds the right view and uses hide-unhide instead. UIView *rightView = [self rightView]; if (rightView != nil && [self clearButtonMode] == UITextFieldViewModeNever) { BOOL showRightView; BOOL isFirstResponder = [self isFirstResponder]; switch ([self rightViewMode]) { case UITextFieldViewModeNever: showRightView = FALSE; break; case UITextFieldViewModeWhileEditing: showRightView = isFirstResponder; break; case UITextFieldViewModeUnlessEditing: showRightView = !isFirstResponder; break; case UITextFieldViewModeAlways: default: showRightView = TRUE; break; } showRightView = (showRightView && ![[self text] isEqualToString:@""]); [rightView setFrame:[self rightViewRectForBounds:[self bounds]]]; [rightView setHidden:!showRightView]; [self addSubview:rightView]; } } 
+1
source
 - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString { UITextField *searchField = nil; for (UIView *subview in controller.searchBar.subviews) { DebugLog(@"%@",[subview description]); if ([subview isKindOfClass:[UITextField class]]) { searchField = (UITextField *)subview; UIImageView *clearIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ClearIcon.png"]]; searchField.rightView = clearIconView; searchField.rightViewMode = UITextFieldViewModeAlways; [clearIconView release]; break; } } } 
0
source

Simple code to solve this problem

 - (void)textFieldDidBeginEditing:(UITextField *)textField { textField.rightViewMode=UITextFieldViewModeAlways; } - (BOOL)textFieldShouldEndEditing:(UITextField *)textField { textField.rightViewMode=UITextFieldViewModeNever; return YES; } 
0
source

I wrote an open source class, STAResizingTextField , which allows you to specify custom button images with a clear text box.

0
source

Source: https://habr.com/ru/post/895698/


All Articles