View not moving up when keyboard appears
本问题已经有最佳答案,请猛点这里访问。
我在子视图中有一个ui文本字段。我正在将它添加到视图中。当键盘弹出时,我想使我要添加此子视图的视图具有动画效果。我只在子视图类中编写了所有textfield委托函数。因此,如果我使用animate文本字段函数,它不会移动父视图,而会使子视图具有动画效果……请帮助我
试试这个:
1 2 3 4 5 6 7 8 | [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; |
在
然后这个
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | - (void)keyboardWillHide:(NSNotification *)aNotification { // the keyboard is hiding reset the table's height NSTimeInterval animationDuration = [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]; CGRect frame = self.view.frame; frame.origin.y += 160; [UIView beginAnimations:@"ResizeForKeyboard" context:nil]; [UIView setAnimationDuration:animationDuration]; self.view.frame = frame; [UIView commitAnimations]; } - (void)keyboardWillShow:(NSNotification *)aNotification { // the keyboard is showing so resize the table's height NSTimeInterval animationDuration = [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]; CGRect frame = self.view.frame; frame.origin.y -= 160; [UIView beginAnimations:@"ResizeForKeyboard" context:nil]; [UIView setAnimationDuration:animationDuration]; self.view.frame = frame; [UIView commitAnimations]; } |
在视图控制器中
最有可能的情况是,您必须根据您的具体视图更改我在此处放置的值(160)。
你试过textfielddidbgineding了吗?我不知道你是否试过,它是否是正确的方法。我用这个,它是少工作线来实现
1 2 3 4 5 6 | -(void)textFieldDidBeginEditing:(UITextField *)textField { self.view.frame=CGRectMake(0, -300, 320, 700); } |
这将把根视图移到顶部,这样子视图将自动移到顶部文本字段将不会隐藏在键盘后面,对不起,我没有评论的声誉