我有一个UIView,它不在UIScrollView中.当键盘出现时,我想要上移我的视图.在我try 使用这个解决方案之前:How can I make a UITextField move up when the keyboard is present?.
它工作得很好.但在文本字段中插入数据后,它会将我带到另一个视图,当我回到这个页面时,它只是跳转,我看不到我的文本字段.这个问题有更好的解决方案吗?
我有一个UIView,它不在UIScrollView中.当键盘出现时,我想要上移我的视图.在我try 使用这个解决方案之前:How can I make a UITextField move up when the keyboard is present?.
它工作得很好.但在文本字段中插入数据后,它会将我带到另一个视图,当我回到这个页面时,它只是跳转,我看不到我的文本字段.这个问题有更好的解决方案吗?
使用以下代码来显示和隐藏键盘
//Declare a delegate, assign your textField to the delegate and then include these methods
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
[self.view endEditing:YES];
return YES;
}
- (void)keyboardDidShow:(NSNotification *)notification
{
// Assign new frame to your view
[self.view setFrame:CGRectMake(0,-110,320,460)]; //here taken -110 for example i.e. your view will be scrolled to -110. change its value according to your requirement.
}
-(void)keyboardDidHide:(NSNotification *)notification
{
[self.view setFrame:CGRectMake(0,0,320,460)];
}