UISearchbar在键盘出现之前做了一些事情

时间:2014-08-18 13:39:23

标签: ios objective-c uisearchbar

我有一个UISearchBar。现在当我点击搜索栏时,键盘出现了。

我需要在键盘出现之前做点什么。我的布局发生了变化。

那么在键盘显示之前有什么方法可以做某事吗?

我想也许是这样,但它不起作用:

-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
    [_searchBar resignFirstResponder];

    return YES;
}

但键盘并没有隐藏。

有任何建议吗?

4 个答案:

答案 0 :(得分:1)

您可以在返回YES或NO之前在searchBarShouldBeginEditing中执行UI更改。 如果您不想显示键盘返回NO。

-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
   // Do the changes in UI

    return YES;
}

答案 1 :(得分:0)

您可以使用NSNotificationCenter在键盘显示和隐藏时收到通知。

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onKeyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onKeyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

然后添加将被调用的两个方法:

- (void)onKeyboardWillShow:(NSNotification *)notification
{   
    NSLog(@"Keyboard will show!");
}

- (void)onKeyboardWillHide:(NSNotification *)notification
{
    NSLog(@"Keyboard will hide!");
}

离开视野时不要忘记停止听:

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];

    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

答案 2 :(得分:0)

如果你想隐藏键盘:

上仅使用此viewwillAppear方法
[searchBarObje resignFirstResponsder];

您只需致电:以显示

[searchBarObje becomeFirstResponsder];

or you can use 
-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
   // Do the changes in UI
   [searchBar becomeFirstResponsder];
    return YES;
}

我想。,这会对你有帮助。

答案 3 :(得分:0)

您可以使用以下标准iOS通知执行此操作:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown) name:UIKeyboardWillShowNotification object:nil];