IOS solves the problem that forms are covered by the keyboard, while ios forms and keyboards are covered

Source: Internet
Author: User

IOS solves the problem that forms are covered by the keyboard, while ios forms and keyboards are covered
Problem

When processing a form, you will surely encounter the problem that the input control is covered by the keyboard ,:

Instance

The left side is a normal form, the middle side is a 2B form, and the right side is a literary form.

Analysis

There are two steps to solve this problem:

The code is written in this step.

How can we implement these steps Step by Step?

Capture Keyboard Events

Capture Keyboard Events

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(actionKeyboardShow:)
name:UIKeyboardDidShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(actionKeyboardHide:)
name:UIKeyboardWillHideNotification
object:nil];

- (void)actionKeyboardShow:(NSNotification *)notification
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidChangeFrameNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(actionKeyboardShow:)
name:UIKeyboardDidChangeFrameNotification
object:nil];

}

- (void)actionKeyboardHide:(NSNotification *)notification
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidChangeFrameNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(actionKeyboardShow:)
name:UIKeyboardDidShowNotification
object:nil];
}
Calculate and adjust the keyboard height UITableViewOf frame

Calculate the keyboard height and adjust the frame of UITableView

1
2
3
4
5
6
7
8
9
10
11
12
13
- (void)actionKeyboardShow:(NSNotification *)notification
{
CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size
self.tableView.frame = CGRectMake(0, 0, 320, self.view.h-keyboardSize.height);

[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidChangeFrameNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(actionKeyboardShow:)
name:UIKeyboardDidChangeFrameNotification
object:nil];

}
Obtain the control currently being input

Here, we have to say that common programmers generally obtain this information.

Category of UIView

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
- (UIView *) getFirstResponder
{
if (self.isFirstResponder) {
return self;
}

for (UIView *subView in self.subviews) {
UIView *firstResponder = [subView getFirstResponder];
if (firstResponder != nil) {
return firstResponder;
}
}

return nil;
}

Although it is true, it should be obtained by art programmers like this.

UIResponder Category

1
2
3
4
5
6
7
8
9
10
11
static __weak id currentFirstResponder;

+(id)currentFirstResponder {
currentFirstResponder = nil;
[[UIApplication sharedApplication] sendAction:@selector(findFirstResponder:) to:nil from:nil forEvent:nil];
return currentFirstResponder;
}

-(void)findFirstResponder:(id)sender {
currentFirstResponder = self;
}

Likewise, there are three options for removing the keyboard.

1
2
3
4
5
[someView resignFirstResponder];

[self.view endEditing:YES];

[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];

How to choose? It's up to U.

Calculate UITableViewAnd scroll to its position to make it visible.

Calculate its position in UITableView and scroll to its position to make it visible.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
- (void)actionKeyboardShow:(NSNotification *)notification
{
CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
self.tableView.frame = CGRectMake(0, 0, 320, self.view.h-keyboardSize.height);

UIView *v = [UIResponder currentFirstResponder];

if ( v )
{
while ( ![v isKindOfClass:[UITableViewCell class]]) {
v = v.superview;
}

UITableViewCell *cell = (UITableViewCell*)v;

[self.tableView scrollToRowAtIndexPath:[self.tableView indexPathForRowAtPoint:cell.center] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}

[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidChangeFrameNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(actionKeyboardShow:)
name:UIKeyboardDidChangeFrameNotification
object:nil];

}

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.