IOS 调用系统键盘 设置搜索字段和事件

其实说白了,就是将系统的return 键换成搜索,然后对renturn 事件作处理

通过红色的设置,就可以在系统键盘(系统键盘或者搜狗输入法)上显示“搜索”字段了。

 UIImageView *_seachView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"search"]];
    _seachView.frame = CGRectMake(15, 50, 10, 10);
    _searchTextField = [self generateTextField];
    _searchTextField.placeholder = @"  请输入司机的卡号或手机号";
    [_searchTextField setFrame:CGRectMake(15, 0, kDEVICEWIDTH - 100, 30)];
    _searchTextField.leftView = _seachView;
    _searchTextField.layer.borderColor = ([[CHUtil colorWithHexString:@"c7c7c9"]CGColor]);
    [_searchTextField setValue:[CHUtil colorWithHexString:@"c5c5c5"] forKeyPath:@"_placeholderLabel.textColor"];
    [_searchTextField addTarget:self action:@selector(searchTextFieldChangeAction:)
               forControlEvents:UIControlEventEditingChanged];
   <span style="color:#ff0000;"> _searchTextField.returnKeyType = UIReturnKeySearch; //设置按键类型
    _searchTextField.enablesReturnKeyAutomatically = YES; //这里设置为无文字就灰色不可点</span>
    
    [_contentView addSubview:_searchTextField];

获取搜索的点击事件:红色就是处理搜索点击的事件。

#pragma mark -- UITextFieldDelegate method
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self resignTextResponder];
}

<span style="color:#ff0000;">- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self resignTextResponder];
    
    if(textField.returnKeyType==UIReturnKeySearch)
    {
        if ([_searchTextField.text length] > 0)
        {
            [self dealWithSearchText:_searchTextField.text];
        }
    }
    return YES;
}</span>


- (void)resignTextResponder
{
    if ([_searchTextField isFirstResponder])
    {
        [_searchTextField resignFirstResponder];
    }
}


你可能感兴趣的:(IOS 调用系统键盘 设置搜索字段和事件)