当键盘出现在文本字段上时,我有以下代码用于上下移动视图。
-(void)textFieldDidBeginEditing:(UITextField *)textField{
NSLog(@"height before animation%f",self.view.frame.size.height);
NSLog(@"%f",self.view.frame.size.height);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
[self.view setFrame:CGRectMake(0,-216,320,460)];
[UIView commitAnimations];
}
-(void)textFieldDidEndEditing:(UITextField *)textField{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
[self.view setFrame:CGRectMake(0,0,320,460)];
[UIView commitAnimations];
NSLog(@"height after animation %f",self.view.frame.size.height);
}
这是我在键盘出现然后编辑完成后得到的示例日志:
2014-01-21 11:00:51.194 Master-view[456:70b] height before animation 568.000000
2014-01-21 11:00:53.635 Master-view[456:70b] height after animation 460.000000
视图的高度似乎降低了,这使得屏幕的底部不可互动。为什么会这样?
视图正在向上移动并且正在下降而没有问题。在视觉上似乎没有区别。在升空之前存在的所有元素都在下降后存在。但是屏幕底部的元素(超出460.0的高度)并不难解决。
答案 0 :(得分:6)
-(void)textFieldDidBeginEditing:(UITextField *)textField{
NSLog(@"height before animation%f",self.view.frame.size.height);
NSLog(@"%f",self.view.frame.size.height);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
[self.view setFrame:CGRectMake(0,-216,320,self.view.frame.size.height)];
[UIView commitAnimations];
}
-(void)textFieldDidEndEditing:(UITextField *)textField{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
[self.view setFrame:CGRectMake(0,0,320,self.view.frame.size.height)];
[UIView commitAnimations];
NSLog(@"height after animation %f",self.view.frame.size.height);
}
我根据视图高度设置框架,你只需在按键开始时改变y位置,并在结束编辑时尝试将其设置回来 这可能对你有所帮助
答案 1 :(得分:0)
尝试更改
-(void)textFieldDidBeginEditing:(UITextField *)textField{
NSLog(@"height before animation%f",self.view.frame.size.height);
NSLog(@"%f",self.view.frame.size.height);
CGRect viewFrame = self.view.frame;
viewFrame.origin.y = -216;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
-(void)textFieldDidEndEditing:(UITextField *)textField{
CGRect viewFrame = self.view.frame;
viewFrame.origin.y = 0;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
NSLog(@"height after animation %f",self.view.frame.size.height);
}
答案 2 :(得分:0)
我们使用NSNotification键盘将显示,键盘将隐藏。
我们使用键盘节目的添加通知并隐藏我们在viewWillAppear中添加通知并删除viewWillDisappear中的通知。
-(void)viewWillAppear:(BOOL)animated{**
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)viewWillDisappear:(BOOL)animated**
{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
}
-(void)keyboardWillShow {
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];
}
}
-(void)setViewMovedUp:(BOOL)movedUp
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
CGRect rect = self.view.frame;
if (movedUp)
{
rect.origin.y -= kOFFSET_FOR_KEYBOARD;
rect.size.height += kOFFSET_FOR_KEYBOARD;
}
else
{
rect.origin.y += kOFFSET_FOR_KEYBOARD;
rect.size.height -= kOFFSET_FOR_KEYBOARD;
}
self.view.frame = rect;
[UIView commitAnimations];
}