我試圖在鍵盤出現時將視圖向上移動,然後再次消失。 第一次出現鍵盤時,它按預期工作。當按'完成'時,鍵盤消失並且視圖移回。但是,在初始編輯之後,單擊文本文件不會執行任何操作。鍵盤根本沒有出現。UITextField:第一次編輯後鍵盤不出現
在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];
}