当键盘出现在 iOS 中时将 UIView 向上移动 [英] Move UIView up when the keyboard appears in iOS
问题描述
我有一个 UIView,它不在 UIScrollView 中.当键盘出现时,我想向上移动我的视图.在我尝试使用此解决方案之前:如何当键盘存在时,我让 UITextField 向上移动吗?.
I have a UIView, it is not inside UIScrollView. I would like to move up my View when the keyboard appears. Before I tried to use this solution: How can I make a UITextField move up when the keyboard is present?.
它运行良好.但是在文本字段中插入数据后,它把我带到另一个视图,当我回到这个页面时,它只是跳跃,我看不到我的文本字段.这个问题有没有更好的解决方案?
It was working fine. But after inserting the data in the textfields, it takes me to another view, and when I get back to this page, its just jumping, and I couldn't see my text fields. Is there any better solution for this problem?
推荐答案
使用以下代码来显示和隐藏键盘
Use the following code in order to show and hide the keyboard
//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)];
}
这篇关于当键盘出现在 iOS 中时将 UIView 向上移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!