平时在开发中,当有输入框时,弹出的键盘有时会遮挡住下方的屏幕,为此我们可以在对输入框进行操作时使屏幕上移,避免下方屏幕被遮挡。(也可以使用ScrollView进行滑动。)
#pragma mark - 屏幕上弹
-(
void
)textFieldDidBeginEditing:(UITextField *)textField
{
//键盘高度216
//滑动效果(动画)
NSTimeInterval animationDuration = 0.30f;
[UIView beginAnimations:@
"ResizeForKeyboard"
context:nil];
[UIView setAnimationDuration:animationDuration];
//将视图的Y坐标向上移动,以使下面腾出地方用于软键盘的显示
self.view.frame = CGRectMake(0.0f, -100.0f, self.view.frame.size.width, self.view.frame.size.height);
//64-216
[UIView commitAnimations];
}
#pragma mark -屏幕恢复
-(
void
)textFieldDidEndEditing:(UITextField *)textField
{
//滑动效果
NSTimeInterval animationDuration = 0.30f;
[UIView beginAnimations:@
"ResizeForKeyboard"
context:nil];
[UIView setAnimationDuration:animationDuration];
//恢复屏幕
self.view.frame = CGRectMake(0.0f, 0.0f, self.view.frame.size.width, self.view.frame.size.height);
//64-216
[UIView commitAnimations];