I had an issue with ios where when keyboard shows the whole UI gets pushed up out of the view. Then I fixed it using one of the solutions found here. It solved the issue but there is gap between the textarea & the keyboard when keyboard appears. The app I'm working on is an hybrid app which is basically angularJS, CSS & html. I have 0 knowledge about objective C & how it works. If anyone could help me out to solve this issue it would be great help. Will share the screenshot as well as objective-c code that I have added to solve the UI getting pushed to the top.
Basically when the keyboard is not present the comment box is always at the bottom & has CSS property of position:fixed & bottom:0. MainViewController.m
@interface MainViewController ()
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@end
#define DEVICE_HEIGHT [[UIScreen mainScreen] bounds].size.height
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:self.view.window];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:self.view.window];
}
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
// unregister for keyboard notifications while not visible.
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification
object:self.view.window];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:self.view.window];
}
-(void)keyboardWillShow:(NSNotification *)noti
{
NSDictionary* userInfo = [noti userInfo];
CGRect keyboardRect = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]
CGRectValue];
keyboardRect = [self.view convertRect:keyboardRect fromView:nil];
CGRect viewFrame = self.view.frame;
viewFrame.size.height = DEVICE_HEIGHT - CGRectGetHeight(keyboardRect);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.1];
[UIView setAnimationBeginsFromCurrentState:YES];
self.view.frame = viewFrame;
CGPoint bottomOffset = CGPointMake(0, self.scrollView.contentSize.height
- self.view.bounds.size.height);
[self.scrollView setContentOffset:bottomOffset animated:NO];
[UIView commitAnimations];
}
-(void)keyboardWillHide:(NSNotification *)noti
{
NSDictionary* userInfo = [noti userInfo];
CGRect keyboardRect = [[userInfo
objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue];
keyboardRect = [self.view convertRect:keyboardRect fromView:nil];
CGRect viewFrame = self.view.frame;
viewFrame.size.height = DEVICE_HEIGHT;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.1];
[UIView setAnimationBeginsFromCurrentState:YES];
self.view.frame = viewFrame;
[UIView commitAnimations];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
MainViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UITextFieldDelegate>
@end