[IOS] how to work with uiscrollview and keypad-click a blank space to hide the keypad (perfect solution, no code added)

Source: Internet
Author: User
Tags uicontrol

Author: zyl910

"Hide the keyboard in the blank space" is a common function. The simplest way is to set the background view as a uicontrol class, and then hide the soft keyboard in the touch down event. For details, see http://www.cnblogs.com/zyl910/archive/2013/03/29/ios_textfield_keyboard.html.
However, when the text box is placed in the uiscrollview control, the above method becomes invalid. Uiscrollview itself does not provide touch events, but it intercepts touch operations so that the background view cannot receive touch events.
What should I do?

I once found a solution on the network-writing a derived class of uiscrollview to provide touch events. This solution has many disadvantages, such as complicated code, and a touch event may conflict with the drag and scroll function of uiscrollview. Low availability.

Today, I suddenly thought of a perfect solution. You only need to add a control on the interface, without adding code. You can use the original "Click blank space in view to hide the soft keyboard" code. Also, hiding the soft keyboard operation does not conflict with the drag and scroll function of uiscrollview. Haha!

The above boast so much, in fact, the solution is very simple, similar to the brain sharp turns.
Specific solution --
1. Put a view in uiscrollview.
2. Place various controls in the view.
3. Change the Class Name of the view to uicontrol to disable the keyboard when handling the touch down event.

I was worried that the view above uiscrollview would intercept touch events, so that uiscrollview could not be dragged and rolled normally. However, after running the SDK, you can see that the effect is perfect.
This is because uiscrollview preemptible the touch event. when it determines that it is not dragging or rolling, it will give up the preemption of the touch event so that the view above receives the touch event.

Next, I found a simpler code for disabling the keyboard --

- (IBAction)view_TouchDown:(id)sender {    [self.view endEditing:YES];}

 

Finally, I learned how to handle the problem of Soft Keyboard occlusion.
When the keyboard is displayed, uiscrollview is not automatically reduced to a non-soft keyboard area, which blocks the lower half and cannot be operated.
To solve this problem, the general method is to receive the keyboard display/hide notifications, manually calculate the size of the non-soft keyboard area, and then set the uiscrollview position. This method is not only complex in code, but also difficult to adapt to the Landscape mode and many controls.
I am doing this-set the content insets bottom of uiscrollview to a slightly larger value with the height of the view, that is, to expand the height of the content, so that there is enough white space to show the soft keyboard. In this way, although not exquisite, it can also solve the occlusion problem.



Related Article

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.