UITextField:首次编辑后键盘没有出现

时间:2014-01-16 14:14:02

标签: ios objective-c

我正试图在键盘出现时向上移动我的视图,然后再次向下移动消失。 键盘第一次出现时,它按预期工作。当我按下“完成”时,键盘消失,视图向后移动。但是,在初始编辑之后,单击textfiled不会执行任何操作。键盘根本没有出现。

我写的viewDidLoad方法:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];

并实施了以下代表:

- (void)keyboardDidShow:(NSNotification *)notification
{
    //Assign new frame to your view
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
    NSDictionary* info = [notification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    [self.view setFrame:CGRectMake(0,-kbSize.height,320,460)];

    [UIView commitAnimations];

}
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
   [self.view endEditing:YES];
    return NO;
}

-(void)keyboardDidHide:(NSNotification *)notification
{   [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
    [self.view setFrame:CGRectMake(0,0,320,460)];
    [UIView commitAnimations];
}

3 个答案:

答案 0 :(得分:0)

我不建议你为它移动self.view。将所有子视图放在scrollview或tableview上,并在其上指定此类。

https://github.com/michaeltyson/TPKeyboardAvoiding

答案 1 :(得分:0)

您需要在UITextfield上实现resignFirstResponder,以便可以再次启动键盘。在您的实现中,您可以将以下行添加到textFieldShouldReturn方法:

[textfield resignFirstResponder];

答案 2 :(得分:0)

如果您将-textFieldShouldReturn方法更改为以下方法,则应该有效。

-(BOOL)textFieldShouldReturn:(UITextField *)textField {
   [textField resignFirstResponder];
   return YES;
}

根据文件:

  

通过返回NO,您可以阻止用户切换到另一个控件

返回NO,继续关注控件,所以我假设下次你点击textFiled没有任何反应,因为系统认为控件已经集中。

相关问题