How to move View up when Keyboard come over the TextFields? - ios

i am having 10 textfields over the scrollView. I want to scroll the view when keyboard Appears on Textfileds . Here i am using the below code to achieve this. But it is moving the screen up from the First Textfield my requirement is i want to move view up when keyboard come over the textfield only otherwise i don't want to move the view up.
Please help me.
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
CGPoint scrollPoint = CGPointMake(0, textField.frame.origin.y);
[myScrollView setContentOffset:scrollPoint animated:YES];
}

Something like this maybe? (You may have to readapt it for landscape mode)
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
CGFloat keyboardHeight = 216.0;
CGFloat viewHeight = myScrollView.frame.size.height;
BOOL covering = (textField.frame.origin.y - myScrollView.contentOffset.y) > (viewHeight - keyboardHeight);
if(covering) {
CGPoint scrollPoint = CGPointMake(0, textField.frame.origin.y);
[myScrollView setContentOffset:scrollPoint animated:YES];
}
}

There are ready solutions for this.
You could use TPKeyboardAvoiding to accomplish your mission.

Use keyboard notifications to be alerted when the keyboard is presented e.g.:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
(Also remove these in viewWillDissapear:
[[NSNotificationCenter defaultCenter] removeObserver:UIKeyboardWillShowNotification];
[[NSNotificationCenter defaultCenter] removeObserver:UIKeyboardWillHideNotification];
Then in the functions respectively:
- (void)keyboardWillShow:(NSNotification *) notification {
CGRect appFrame = YOUR_VIEWS_FRAME
CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
keyboardFrame = [self.view.window convertRect:keyboardFrame toView:self.view];
UITextField *current = THE_CURRENT_RESPONDER
CGRect relativeFrame = [self.view convertRect:current.frame fromView:current.superview];
float difference = 0.0;
//Here we give it a 20 pixel padding between the top of the keyboard and the bottom of the text field.
if (CGRectIntersectsRect(relativeFrame, keyboardFrame))
difference = appFrame.size.height - keyboardFrame.size.height - 20 - relativeFrame.size.height - current.frame.origin.y;
difference = difference < 0 ? difference : 0;
NSTimeInterval duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
[UIView animateWithDuration:duration animations:^{
[self.scrolLView setContentOffset:CGPointMake(0, difference);
} completion:^(BOOL finished) {
}];
}
And:
- (void)keyboardWillHide:(NSNotification *) notification {
NSTimeInterval duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
[UIView animateWithDuration:duration animations:^{
[self.scrolLView setContentOffset:CGPointZero];
} completion:^(BOOL finished) {
}];
}

Related

SCLAlertView don't move when keyboard shows

I'm using SCLAlertView and add custom view with UITextView, my problem is when showing keyboard at first time the SCLAlertView not moving, see this picture
I write this code to move SCLAlertView when keyboard will show
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShowFirstView:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHideFirstView:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
- (void)keyboardWillShowFirstView:(NSNotification *)notification
{
CGSize screenSize = [UIScreen mainScreen].bounds.size;
CGRect dialogRect = addAlert.view.frame;
CGFloat keyboardHeight = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
CGFloat top = (screenSize.height - keyboardHeight - dialogRect.size.height) / 2;
NSLog(#"show........: %.0f", top);
[UIView animateWithDuration:0.2 animations:^{
CGRect frame = addAlert.view.frame;
frame.origin.y = top;
addAlert.view.frame = frame;
}];
}
- (void)keyboardWillHideFirstView:(NSNotification *)notification
{
CGSize screenSize = [UIScreen mainScreen].bounds.size;
CGRect dialogRect = addAlert.view.frame;
CGFloat top = (screenSize.height - dialogRect.size.height) / 2;
NSLog(#"...hide.....: %.0f", top);
[UIView animateWithDuration:0.2 animations:^{
CGRect frame = addAlert.view.frame;
frame.origin.y = top;
addAlert.view.frame = frame;
}];
}
Now when I hide & show keyboard manually in simulator "Command+K" it's moving right!!
Can someone help to fix that!
Try to hook the centerY constraint of the AlertView as IBOutlet and change it's constant as the keyboard is shown/Hidden
like this
(void)keyboardWillShowFirstView:(NSNotification *)notification
{
CGSize screenSize = [UIScreen mainScreen].bounds.size;
CGRect dialogRect = addAlert.view.frame;
CGFloat keyboardHeight = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
CGFloat top = -1 * (keyboardHeight / 2.0);
self.alertViewCenterY.constant = top
[UIView animateWithDuration:0.2 animations:^{
[self.view layoutIfNeeded];
}];
}
- (void)keyboardWillHideFirstView:(NSNotification *)notification
{
self.alertViewCenterY.constant = 0
[UIView animateWithDuration:0.2 animations:^{
[self.view layoutIfNeeded];
}];
}
Note: You may hook Top or bottom constraint If you want

manage view with keyboard and autolayout in center

I have the following view hierarchy. View->container->2 UITextfields and 1 button in containerView. The container is in the center of the screen. What I want to do is to move the container up when keyboard appears and UITextfield is behind the keyboard and move back to center when keyboard disappear. Here is the screenshot for the same.
What constraints do I need to change or do I need to add constraints in code ?
Get the frame of the container while keyboard show and update the new frame size. 'setTranslatesAutoresizingMaskIntoConstraints' is the right solution while updating frame of a view.It worked for me
#implementation ViewController
{
CGRect defaultFrame;
}
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
#pragma mark Notifications
- (void)keyboardWillShow:(NSNotification *)notification {
CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
defaultFrame = self.ContentView.frame;
[self.ContentView setTranslatesAutoresizingMaskIntoConstraints:YES];
[self.ContentView layoutIfNeeded];
CGRect contentInsets = CGRectMake(defaultFrame.origin.x, (keyboardSize.height), defaultFrame.size.width, defaultFrame.size.height);
[UIView animateWithDuration:0.5f
animations:^{
self.ContentView.frame = contentInsets;
}
completion:^(BOOL finished){
}
];
self.ContentView.frame = contentInsets;
}
- (void)keyboardWillHide:(NSNotification *)notification {
[self.ContentView setTranslatesAutoresizingMaskIntoConstraints:NO];
[UIView animateWithDuration:0.5f
animations:^{
self.ContentView.frame = defaultFrame;
}
completion:^(BOOL finished){
}
];
}
As I understand the question, you are trying to shift the entire view when the UITextField becomes the first responder (i.e. adds the keyboard to the view)? If that is the case, I would add code in the UITextField delegate method:
#define VIEW_TAG 12345
#define kKeyboardOffsetY 80.0f
- (void)textFieldDidBeginEditing:(UITextField *)textField {
// get a reference to the view you want to move when editing begins
// which can be done by setting the tag of the container view to VIEW_TAG
UIView *containerView = (UIView *)[self.view viewWithTag:VIEW_TAG];
[UIView animateWithDuration:0.3 animations:^{
containerView.frame = CGRectMake(0.0f, -kKeyboardOffsetY, containerView.frame.size.width, containerView.frame.size.height);
}];
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
UIView *containerView = (UIView *)[self.view viewWithTag:VIEW_TAG];
[UIView animateWithDuration:0.3 animations:^{
containerView.frame = CGRectMake(0.0f, self.view.frame.origin.y, containerView.frame.size.width, containerView.frame.size.height);
}];
}
add UITextFieldDelegate in .h file
and try the following code it will surely help you
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if (textField == username)
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardDidShowNotification object:nil];
}
if (textField == password)
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardDidShowNotification object:nil];
}
return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
if (textField == username)
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide:) name:UIKeyboardDidHideNotification object:nil];
}
if (textField == password)
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide:) name:UIKeyboardDidShowNotification object:nil];
}
return YES;
}
- (void)keyboardWillShow:(NSNotification *)notification
{
CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
float newVerticalPosition = -keyboardSize.height + 100;
[self moveFrameToVerticalPosition:newVerticalPosition forDuration:0.3f];
}
- (void)keyboardWillHide:(NSNotification *)notification
{
CGFloat kNavBarHeight = self.navigationController.navigationBar.frame.size.height;
[self moveFrameToVerticalPosition:kNavBarHeight forDuration:0.3f];
}
- (void)moveFrameToVerticalPosition:(float)position forDuration:(float)duration
{
CGRect frame = self.view.frame;
frame.origin.y = position;
[UIView animateWithDuration:duration animations:^{
self.view.frame = frame;
}];
}
Take IBOutlet of your bottom constraint or top constraint of container view by ctrl+drag from respactive constraint.
If bottom constraint then increase the constant of constraint equal to keyboard height when keyboard appears and decrease same when keyboard disappear.
If using top constraint then decrease the constant equal to keyboard height when keyboard appears and increase same when keyboard disappear.
for example,
topConstraint.constant = topConstraint.constant + keyboardHeight;
Update :
According to your constraint you should take outlet of vertically center or center x and you should do something like,
self.verticallyCenter.constant = self.horizontalyCenter.constant - 100 //here 100 is keyboardheight for example and do it in viewdidload
By this your container view will goes up to 100 pixels.
when resign keyboard you can add 100 to it's constant to get initial position back.
Hope this will help :)
Seems like you have given correct constraints all you need to do now is to make a outlet of centre y constraint and change it's constant on keyboard show/hide events, Which you can grab with keyboardDidShow/keyboardWillShow and keyboardDidHide/keyboardWillHide notifications. that said you can also add cool animation effects for these changes. Try out let me know if you need example.
Edit:
add your viewcontroller as observer for two Notifications:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardDidHide:)
name:UIKeyboardDidHideNotification
object:nil];
Now suppose name of your constraint is "constraintForTopSpace",then add two methods for the notifications:
- (void)keyboardWasShown:(NSNotification *)notification {
// Get the size of the keyboard.
CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
//Given size may not account for screen rotation
int height = MIN(keyboardSize.height,keyboardSize.width);
// Here you can set your constraint's constant to lift your container up.
[UIView animateWithDuration:0.5 animations:^{
[constraintForTopSpace setConstant:constraintForTopSpace.constant - height];
[self.view layoutIfNeeded];
}];
}
- (void)keyboardDidHide:(NSNotification *)notification {
CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
int height = MIN(keyboardSize.height,keyboardSize.width);
// Here you can set your constraint's constant to move your container down.
[UIView animateWithDuration:0.5 animations:^{
[constraintForTopSpace setConstant:constraintForTopSpace.constant + height];
[self.view layoutIfNeeded];
}];
}
And you can also use UIKeyboardWillShowNotification/UIKeyboardWillHideNotification notification they will be triggered before the keyboard appears on screen, Its up to your requirements.
Here adding animation will give smooth look and feel. ;)

Enable scrolling content when keyboard appears in iOS

I have an application on iPhone who has much content in viewcontroller. Generally the main content is form. My problem is that I put up keyboard and want to type some value to form fields much of content is invisible. I want to scroll this content when keyboard appear. How can I do this?
My view does'n have scroll view however I tried do this but it don't works.
All hints I found so far concern how to put up used textbox, but this is not my assumption.
This is what i did, I changed the top constraint when keyboard rises up and down like this-
in viewDidLoad call this method-
[self observeKeyboard];
#pragma mark- Keyboard Listen Methods
- (void)observeKeyboard {
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardWillChangeFrameNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)keyboardWillShow:(NSNotification *)notification {
NSDictionary *info = [notification userInfo];
NSValue *kbFrame = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
NSTimeInterval animationDuration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
CGRect keyboardFrame = [kbFrame CGRectValue];
CGFloat height = keyboardFrame.size.height;
self.COnstraintTopViewVertical.constant=(value of constant)-height;
[UIView animateWithDuration:animationDuration animations:^{
[self.view layoutIfNeeded];
}];
}
- (void)keyboardWillHide:(NSNotification *)notification {
NSDictionary *info = [notification userInfo];
NSTimeInterval animationDuration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
self.ConstraintTopViewVertical.constant=reset value;
[UIView animateWithDuration:animationDuration animations:^{
[self.view layoutIfNeeded];
}];
}
You can programmatically change position in the view upon keyboard appearance with the following code:
func keyboardWasShown(notification: NSNotification) {
UIView.animateWithDuration(0.3, animations: { () -> Void in
self.setContentOffset(CGPoint(x: setXOffsetHere, y: setYOffsetHere), animated: true)
})
}
This code will transition the view to the coordinates you specify in .setContentOffset call after keyboard appearance. You still have to get the coordinates, but usually they are pretty straightforward to get from storyboard.
Put all your views in a scroll view
Set an auto layout constraint from the bottom of the scroll view to the bottom of the superview
In viewWillAppear
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillChangeFrame:)
name:UIKeyboardWillChangeFrameNotification object:nil];
Handle the KB notification
- (void)keyboardWillChangeFrame:(NSNotification*)aNotification
{
CGRect kbFrame = [aNotification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGRect kbWindowIntersectionFrame = CGRectIntersection(self.view.window.bounds, kbFrame);
[UIView animateWithDuration:[aNotification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue] animations:^{
self.scrollVBottomConstraint.constant = kbWindowIntersectionFrame.size.height;
[self.view layoutIfNeeded];
}];
}
what i got from your question is: you are using textfield on UIView which is not on the scrollview, now you have to scroll the UIView when keyboard appears. just do following stuff.
-Give the tag to your textfield.
-suppose tag you have given is 4545646
-do not forgot to assign delegate to your textfield (it will execute below delegate method when you start to edit)
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
if (textField.tag == 4545646) {
UIView *contanerView = [self.view viewWithTag:5556466];
[UIView animateWithDuration:0.5 animations:^{
contanerView.frame = CGRectMake(self.view.frame.origin.x+1, -85, self.view.frame.size.width, self.view.frame.size.height-9);
}];
}
}
Change the frame as per your requirement.
and
- (void)textFieldDidEndEditing:(UITextField *)textField {
if (textField.tag == 4545646) {
[textField resignFirstResponder];
UIView *contanerView = [self.view viewWithTag:5556466];
[UIView animateWithDuration:0.5 animations:^{
contanerView.frame = CGRectMake(self.view.frame.origin.x+1, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);
}];
}
In viewDidAppear add the following notifications
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyBoardShown:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyBoardDismiss:) name:UIKeyboardWillHideNotification object:nil];
and in keyBoardShown method get the keyboard height
- (void)keyBoardShown:(NSNotification*)notification
{
if(self.view.frame.origin.y>=0)
{
NSDictionary* keyboardInfo = [notification userInfo];
NSValue* keyboardFrameBegin = [keyboardInfo valueForKey:UIKeyboardFrameBeginUserInfoKey];
CGRect keyboardFrameBeginRect = [keyboardFrameBegin CGRectValue];
// NSLog(#"hihi : %f",keyboardFrameBeginRect.size.height);
keyBoardHeight = keyboardFrameBeginRect.size.height-yDeault+50;
if (keyBoardHeight>0) {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
self.view.frame = CGRectMake(self.view.frame.origin.x, (self.view.frame.origin.y-keyBoardHeight), self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
}
}
}
in keyBoardDismiss setback the mainview y coordinate to previous value
-(void)keyBoardDismiss:(NSNotification*)notification{
if(self.view.frame.origin.y<0){
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
self.view.frame = CGRectMake(self.view.frame.origin.x, (self.view.frame.origin.y+keyBoardHeight), self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
}
}
here yDeault is textfield y coordinate value
yDeault= screenHeight-textField.frame.origin.y+textField.frame.size.height;
Once the keyboard is shown main view will be moved up and once keyboard is dismissed main view will be moved back to the original position
OK. I done this. I need to just add scroll view but in my app probably I have to reconstruct all my view.
No need to add scrollview. Directly you can do this. You can change the self.view.frame without the need of scrollview.

Shift UITextField and UITableView up when keyboard comes up

I have a view with 1. Navigation bar 2.UITableView 3. UITextView.
When I start to edit the textView, a keyboard comes up and I need to animate the TextView and TableView up. I've implemented: https://stackoverflow.com/a/8704371/1808179, but that animated the entire view up, covering the navigation bar.
I tried individually animating the textView like:
- (void)keyboardWillShow:(NSNotification*)notification
{
CGRect chatTextFieldFrame = CGRectMake(chatTextField.frame.origin.x,chatTextField.frame.origin.y-218,chatTextField.frame.size.width,chatTextField.frame.size.height);
[UIView animateWithDuration:0.5 animations:^{ chatTextField.frame = chatTextFieldFrame;}];
}
But it doesn't animate, and it won't synchronously animate alongside the TableView.
What is the best way to animate the tableView and textView up without overlaying the navigation bar?
I generally use the follow snippet when tackling this problem.
Using a UITableView (which is just a subclass of UIScrollView) you should set contentInsets rather then just changing the frame each time. This is especially much nicer in iOS7 with a translucent keyboard.
- (void)viewDidLoad;
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)dealloc;
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
#pragma mark - Keyboard Notifications
- (void)keyboardWillShow:(NSNotification *)notification;
{
NSDictionary *userInfo = [notification userInfo];
NSValue *keyboardBoundsValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGFloat keyboardHeight = [keyboardBoundsValue CGRectValue].size.height;
CGFloat duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
NSInteger animationCurve = [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue];
UIEdgeInsets insets = [[self tableView] contentInset];
[UIView animateWithDuration:duration delay:0. options:animationCurve animations:^{
[[self tableView] setContentInset:UIEdgeInsetsMake(insets.top, insets.left, keyboardHeight, insets.right)];
[[self view] layoutIfNeeded];
} completion:nil];
}
- (void)keyboardWillHide:(NSNotification *)notification;
{
NSDictionary *userInfo = [notification userInfo];
CGFloat duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
NSInteger animationCurve = [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue];
UIEdgeInsets insets = [[self tableView] contentInset];
[UIView animateWithDuration:duration delay:0. options:animationCurve animations:^{
[[self tableView] setContentInset:UIEdgeInsetsMake(insets.top, insets.left, 0., insets.right)];
[[self view] layoutIfNeeded];
} completion:nil];
}
If any are wondering, this is how I did it:
- (void)keyboardWillShow:(NSNotification*)notification
{
CGRect chatTableViewFrame = CGRectMake(0,65,320,chatTableView.frame.size.height-180);
[UIView animateWithDuration:0.3 animations:^{ chatTableView.frame = chatTableViewFrame;}];
CGRect chatTextFieldFrame = CGRectMake(chatTextField.frame.origin.x,chatTextField.frame.origin.y-170,chatTextField.frame.size.width,chatTextField.frame.size.height);
[UIView animateWithDuration:0.3 animations:^{ chatTextField.frame = chatTextFieldFrame;}];
[self.chatTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:self.chat.count-1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}
And vice-versa for keyboardWillHide.
Write this line at desired events.
self.view.frame = [[UIScreen mainScreen] applicationFrame];

Shrink UIWebView when keyboard appears

On my iPhone app i have a UIWebView with toolbars above an below it. the toolbar on the bottom contains a text box for the user to input some text. but when i click on the text box, the keyboard covers the bottom half of the screen.
how do i make it so that the toolbars stay above and below the webview but the the height of the webview shrinks for the keyboard to be displayed?
any guidance on this is appreciated.
thanks in advanced.
To shrink webView you need the following:
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void) viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
- (void)keyboardWillShow:(NSNotification *)notification
{
CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
keyboardFrame = [self.view convertRect:keyboardFrame fromView:self.view.window];
NSTimeInterval keyboardAnimationDuration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
UIViewAnimationOptions keyboardAnimationCurve = [notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue] << 16;
CGFloat keyboardHeight = keyboardFrame.size.height;
[UIView animateWithDuration:keyboardAnimationDuration delay:0 options:keyboardAnimationCurve
animations:^{
_webView.contentInset = UIEdgeInsetsMake(0, 0, keyboardHeight, 0)
}
completion:NULL];
}
- (void)keyboardWillHide:(NSNotification *)notification
{
NSTimeInterval keyboardAnimationDuration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
UIViewAnimationOptions keyboardAnimationCurve = [notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue] << 16;
[UIView animateWithDuration:keyboardAnimationDuration delay:0 options:keyboardAnimationCurve
animations:^{
_webView.contentInset = UIEdgeInsetsZero;
}
completion:NULL];
}
In the case of additional subviews you should slightly change this code (add change of the frames for other subview)
You can implement UITextFieldDelegate in your UIViewController and set the UITextField delegate value to your controller and then implement textFieldDidBeginEditing and textFieldDidEndEditing methods in your controller to detect when editing starts/ends.
- (void)textFieldDidBeginEditing:(UITextField *)textField{
// in case you have more than one text fields in the same view
if(textField == self.YOUR_FIELD_NAME)
// change the web view height here, you can also animate it using UIView beginAnimations
CGRect frame = self.webView.frame;
frame.size.height = 200;
self.webView.frame = frame;
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
// do the opposite here
}

Resources