0

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. The comment box floats away from the keyboard

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
Arun Nair
  • 69
  • 2
  • 9
  • Why don't you use TPKeyboardHandling? No need to write much code. Just download the code and add PKeyboardAvoidingScrollView.h and PKeyboardAvoidingScrollView.m to your project. Then go to storyboard select the Scrollview and set it's class as TPKeyboardAvoidingScrollView. https://github.com/michaeltyson/TPKeyboardAvoiding – kalpa Aug 03 '20 at 06:21
  • @kalpa I will take a look havent heard about it. I find it very difficult to do this as of now I have like 0 experience in how scrollview & storyboard works. That code worked or me so thought could solve it with moderate modifications. – Arun Nair Aug 03 '20 at 06:26
  • 1
    CGPoint bottomOffset = CGPointMake(0, self.scrollView.contentSize.height - self.view.bounds.size.height); Please replace above with CGPoint bottomOffset = CGPointMake(0, self.scrollView.contentSize.height); – kalpa Aug 03 '20 at 06:40
  • Did not checked but what I am assuming. – kalpa Aug 03 '20 at 06:40
  • @kalpa I think the same the problem is at bottomOffset not being calculated correctly. I will try your solution & update. Thanks for the help. – Arun Nair Aug 03 '20 at 06:45
  • @kalpa Tried your solution it did not work still have the same issue. Before this self.scrollView.contentSize.height scrollView was getting undefined property then I made the changes at very top of viewcontroller.m. I think it is not getting the correct vale of scrollView that is why it is not showing the changes. Even commenting that line shows the same result. – Arun Nair Aug 03 '20 at 07:04
  • As you are saying the contentsize is undefined. So I have a question here,how is your structure inside the scrollview?. is it like UIScrollview > Other views or UIScrollview > UIView > Other views. Please checkout this link: https://stackoverflow.com/questions/12846351/uiscrollview-contentsize-not-working – kalpa Aug 03 '20 at 07:53

0 Answers0