资源大全iOS开发技术分享程序员

iOS中输入框随键盘上下移动

2016-09-21  本文已影响763人  逆世界开发者

1.在初始化方法里边注册键盘坐标移动的通知

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];//监听键盘的坐标的变化

2,实现输入框随键盘移动的方法

-(void)keyboardChangeFrame:(NSNotification*)notification

{

//从通知中取出动画速率

intcurve = [[notification.userInfoobjectForKey:UIKeyboardAnimationCurveUserInfoKey]intValue];

//取出动画时间

floatduration = [[notification.userInfoobjectForKey:UIKeyboardAnimationDurationUserInfoKey]floatValue];

//取出键盘动画结束后键盘的frame

CGRectendFrame = [[notification.userInfoobjectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue];

[UIViewanimateWithDuration:durationanimations:^{

[UIViewsetAnimationCurve:curve];

_commentView.frame=CGRectMake(0, endFrame.origin.y-H_i6real(38),self.view.frame.size.width,H_i6real(38));

_commentTableView.frame=CGRectMake(W_i6real(10), endFrame.origin.y-(self.view.frame.size.width/2)-H_i6real(10),self.view.frame.size.width-W_i6real(80+10),self.view.frame.size.width/2-H_i6real(38));

}];

}

上一篇 下一篇

猜你喜欢

热点阅读