Reputation: 13
I'm sorry about my English language.
I tried to find before it acts. But the problem is that ViewController
in the landscape and created UIView
half of the ViewController
. In UIView
have UITextView
. But now when keyboard appear background in the ViewController
scroll down below the keyboard. And see just UIView
. If touch space, keyboard will disappear and background comeback. I want to just is move the UIView
when keyboard appear.
Thank you very much.
Upvotes: 1
Views: 5975
Reputation: 946
try this
- (void)viewDidAppear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)keyboardWillShow:(NSNotification *)note
{
CGRect keyboardBounds;
NSValue *aValue = [note.userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey];
[aValue getValue:&keyboardBounds];
keyboardHeight = keyboardBounds.size.height;
if (!keyboardIsShowing)
{
keyboardIsShowing = YES;
CGRect frame = view.frame;
frame.size.height -= 168;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.3f];
view.frame = frame;
[UIView commitAnimations];
}
}
- (void)keyboardWillHide:(NSNotification *)note
{
CGRect keyboardBounds;
NSValue *aValue = [note.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey];
[aValue getValue: &keyboardBounds];
keyboardHeight = keyboardBounds.size.height;
if (keyboardIsShowing)
{
keyboardIsShowing = NO;
CGRect frame = view.frame;
frame.size.height += 168;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.3f];
view.frame = frame;
[UIView commitAnimations];
}
}
Upvotes: 10
Reputation: 126177
This answer looks like it might be what you're looking for.
In short:
Detect when the keyboard appears with UIKeyboardDidShowNotification.
The user info for that notification describes the frame of the keyboard.
Adjust the frame(s) of your view(s) to get them out from under the keyboard.
Upvotes: 1