为什么在显示键盘时视图向下移动?

为什么在显示键盘时视图向下移动?

问题描述:

我有一个基于标签控制器的应用程序,我有一个uitextview选择哪个移动视图,当键盘出现,整个事情效果很好,直到我切换标签,当我回到这个标签,并尝试编辑uitextview视图向下移动,然后回到键盘出现的正常位置而不是向上移动。这里是整个代码为什么在显示键盘时视图向下移动?

- (void)textViewDidBeginEditing:(UITextView *)textField 
{ 
[self keyboardWillShow]; 

} 
-(void)textViewDidEndEditing:(UITextView *)textField 
{ 
[self keyboardWillHide]; 
} 
-(void)keyboardWillShow { 
// Animate the current view out of the way 
if (self.view.frame.origin.y >= 0) 
{ 
    [self setViewMovedUp:YES]; 
} 
else if (self.view.frame.origin.y < 0) 
{ 
    [self setViewMovedUp:NO]; 
} 
} 

-(void)keyboardWillHide { 
if (self.view.frame.origin.y >= 0) 
{ 
    [self setViewMovedUp:YES]; 
} 
else if (self.view.frame.origin.y < 0) 
{ 
    [self setViewMovedUp:NO]; 
} 
} 
//method to move the view up/down whenever the keyboard is shown/dismissed 
-(void)setViewMovedUp:(BOOL)movedUp 
{ 
[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDuration:0.5]; // if you want to slide up the view 

CGRect rect = self.view.frame; 
if (movedUp) 
{ 
    // 1. move the view's origin up so that the text field that will be hidden come above the keyboard 
    // 2. increase the size of the view so that the area behind the keyboard is covered up. 
    rect.origin.y -= kOFFSET_FOR_KEYBOARD; 
    rect.size.height += kOFFSET_FOR_KEYBOARD; 
} 
else 
{ 
    // revert back to the normal state. 
    rect.origin.y += kOFFSET_FOR_KEYBOARD; 
    rect.size.height -= kOFFSET_FOR_KEYBOARD; 
} 
self.view.frame = rect; 

[UIView commitAnimations]; 
} 

那么我有类似的问题,并找出它。你可以查看我最近的帖子:

https://stackoverflow.com/a/28177645/2989374