iOS 弹出键盘 视图上移

-(void)textFieldDidBeginEditing:(UITextField *)textField

{

CGRect frame = textField.frame;

//在这里我多加了62,(加上了输入中文选择文字的view高度)这个依据自己需求而定

int offset = (frame.origin.y + 20 * widthPropor)- ( self.view.frame.size.height - 216.0);//键盘高度216

//设置动画的名字

[UIView beginAnimations:@"Animation" context:nil];

//设置动画的间隔时间

[UIView setAnimationDuration:0.42];

//使用当前正在运行的状态开始下一段动画

[UIView setAnimationBeginsFromCurrentState: YES];

if (offset>0) {

//将视图的Y坐标向上移动offset个单位,以使下面腾出地方用于软键盘的显示

self.view.frame = CGRectMake(0, -offset, self.view.frame.size.width, self.view.frame.size.height);

}

//设置动画结束

[UIView commitAnimations];

}

-(void)textFieldDidEndEditing:(UITextField *)textField

{

//设置动画的名字

[UIView beginAnimations:@"Animation" context:nil];

//设置动画的间隔时间

[UIView setAnimationDuration:0.42];

//??使用当前正在运行的状态开始下一段动画

[UIView setAnimationBeginsFromCurrentState: YES];

//设置视图移动的位移

self.view.frame = CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height);

//设置动画结束

[UIView commitAnimations];

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容