gpt4 book ai didi

objective-c - UITextField rightView "WhileEditing"问题

转载 作者:可可西里 更新时间:2023-11-01 03:44:46 26 4
gpt4 key购买 nike

我尝试如下子类化 UITextField 以将自定义 rightView 实现为清除按钮:

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

但问题是:当文本字段刚刚成为焦点时,“清除”按钮也变得可见,并且在我开始点击键盘后它消失了。有什么想法吗?

最佳答案

我也遇到了同样的问题。我想这是一个 iOS 错误,但是,我尝试通过以下实现来解决这个问题,它对我来说很好用。希望这对您有所帮助。

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

关于objective-c - UITextField rightView "WhileEditing"问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7165642/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com