Solution for iOS input box being blocked by keyboard

Source: Internet
Author: User

When doing iOS development, you will inevitably encounter the input box is covered by the keyboard problem. On the Internet to search a lot of related solutions, see a lot, but sincerely feel too troublesome.
The solution is to add everything on the view to a scrolling view object (Uiscrollview), and then scroll through the view to make the input box not covered by the soft keyboard, which the individual feels is good, but too troublesome.

有的解决方案是通过一个通知 UIKeyboardDidShowNotification 去实现的,需要用到事件监听,而且需要自己定义并实现“将要开始编辑”与“结束编辑”这两个监听事件中的方法。本人也觉得很麻烦。参考了很多方法,都不是太理想。自己研究了一下,既然软键盘(Keyboard)出现与否是跟输入框(UITextField)紧密关联的。所以自己找到一个解决方案,没有上述两种方案那么麻烦,只需实现代理UITextFieldDelegate中的三个方法即可。

Implementation method:

1) Set the proxy for the input box to self

   (在lb文件中将输入框的delegate设置为File’s Owner 。或者使用代码textField.delegate = self;

2) The Uitextfielddelegate protocol is implemented by the ViewController.h settings corresponding to the input box.

     在ViewController.m文件中实现UITextFieldDelegate的三个方法即可:

[CPP] View plaincopy
When you start editing the input box, the soft keyboard appears, performing this event
-(void) textfielddidbeginediting: (Uitextfield *) TextField
{
CGRect frame = textfield.frame;
int offset = FRAME.ORIGIN.Y +-(self.view.frame.size.height-216.0);//Keyboard Height 216

NSTimeInterval animationDuration = 0.30f; [UIView beginAnimations:@"ResizeForKeyboard" context:nil]; [UIView setAnimationDuration:animationDuration]; //将视图的Y坐标向上移动offset个单位,以使下面腾出地方用于软键盘的显示 if(offset > 0)     

}

Keyboard disappears when the user presses return or presses the ENTER key
-(BOOL) Textfieldshouldreturn: (Uitextfield *) TextField
{
[TextField Resignfirstresponder];
return YES;
}

Restore the view to its original state when the input box is finished editing
-(void) textfielddidendediting: (Uitextfield *) TextField
{
Self.view.frame =cgrectmake (0, 0, self.view.frame.size.width, self.view.frame.size.height);
}
[CPP] View plaincopy
When you start editing the input box, the soft keyboard appears, performing this event
-(void) textfielddidbeginediting: (Uitextfield *) TextField
{
CGRect frame = textfield.frame;
int offset = FRAME.ORIGIN.Y +-(self.view.frame.size.height-216.0);//Keyboard Height 216

NSTimeInterval animationDuration = 0.30f;  [UIView beginAnimations:@"ResizeForKeyboard" context:nil];  [UIView setAnimationDuration:animationDuration];  //将视图的Y坐标向上移动offset个单位,以使下面腾出地方用于软键盘的显示  if(offset > 0)      self.view.frame = CGRectMake(0.0f, -offset, self.view.frame.size.width, self.view.frame.size.height);  [UIView commitAnimations];  

}

Keyboard disappears when the user presses return or presses the ENTER key
-(BOOL) Textfieldshouldreturn: (Uitextfield *) TextField
{
[TextField Resignfirstresponder];
return YES;
}

Restore the view to its original state when the input box is finished editing
-(void) textfielddidendediting: (Uitextfield *) TextField
{
Self.view.frame =cgrectmake (0, 0, self.view.frame.size.width, self.view.frame.size.height);
}

It's a simple method, isn't it? Please note that you must not forget to set the proxy for the input box delegate OH
The implementation effect is as follows:

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Solution for iOS input box being blocked by keyboard

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.