The UIScrollView editing box is displayed with keyboard occlusion.

Source: Internet
Author: User

The UIScrollView editing box is displayed with keyboard occlusion.

When a row in UIScrollView has an editing box, click the edit box. The pop-up keyboard may block the editing box, resulting in poor experience. The solution is simple, that is, to increase the distance between the content of UIScrollView and the inner margin of the UIScrollView container (precisely the bottom margin), which is exactly the keyboard height, the ios system will relocate the selected row, which is exactly the same distance from the bottom of the window. Of course, when you scale back the keyboard, pay attention to setting the padding. The most important function involved is the setContentInset function of UIScrollView.

First, register the keyboard pop-up and scaled back notification listening at the beginning of the program: [[nsicationicationcenter defaultCenter] addObserver: self selector: @ selector (keyboardWillShow :) name: UIKeyboardWillShowNotification object: nil];
[[Nsicationcenter center defacenter center] addObserver: self selector: @ selector (keyboardWillHide :) name: UIKeyboardWillHideNotification object: nil];

 

The implementation of keyboardWillShow and keyboardWillHide is as follows:

-(Void) keyboardWillShow :( NSNotification *) notification
{

// Frame after keyboard pop-up
NSValue * keyboardBoundsValue = [[notification userInfo] objectForKey: UIKeyboardFrameEndUserInfoKey];
CGRect keyboardBounds;
[KeyboardBoundsValue getValue: & keyboardBounds];

// Set the new padding, which is the distance between the last row of UIScrollView and the bottom border of UIScrollView,

// When this function is triggered, the system sets the distance from the selected row to the bottom edge of the window to this value, so that it is not covered by the keyboard.
UIEdgeInsets e = UIEdgeInsetsMake (0, 0, keyboardBounds. size. height, 0 );
[[Self tableView] setContentInset: e];

 

// Adjust the distance between the slide bar and the bottom edge of the window

[[Self tableView] setScrollIndicatorInsets: e];
}

-(Void) keyboardWillHide :( NSNotification *) notification
{

// After the keyboard is scaled back, the settings are restored.
UIEdgeInsets e = UIEdgeInsetsMake (0, 0, 0, 0 );
[[Self tableView] setScrollIndicatorInsets: e];
[[Self tableView] setContentInset: e];
}

As follows:

Before the pop-up:

After the pop-up:

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.