10

Currently, I am working with a container view with an embedded UITableView and am using the IQKeyboardManager CocoaPod to scroll the view so my UITextFields and UITextViews are not covered by the keyboard.

I can successfully import IQkeyboardManager and make it work in other views but it's not working when the UITableView is embedded in a container view.

Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
Kushal Maniyar
  • 846
  • 1
  • 8
  • 26
  • I've clarified your question but there's still not nearly enough information to help. Show the entire code of the class that's doing this. – BaseZen Aug 04 '16 at 15:05

4 Answers4

13
    -(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES]; // This line is needed for the 'auto slide up'
   // Do other stuff
}

Simple solution , No need to create observer.

vivek agravat
  • 251
  • 2
  • 10
10

I've had a similar issue and fixed it using the info given here from the author of the library.

The key statement is:

library logic is to find nearest scrollView from textField. and in your case it's tableView, that is why library chooses tableView to scroll.

So the solution I've used is to disable the UITableView scroll property when the textfield/view is being edited (use the delegate methods), and then re-enable it once editing has finished. This ensures that the library does not detect the UITableView as scrollable and so it ignores it and then moves your container view instead - as you intended. Once the view has moved up as you wanted it to, you can re-enable scrolling via the UIKeyboardWillShowNotification.

So for example, for the UITextField:

-(void) textFieldDidBeginEditing:(UITextField *)textField {
    [self.tableView setScrollEnabled:NO];
}

- (void) textFieldDidEndEditing:(UITextField *)textField {
  [self.tableView setScrollEnabled:YES];
}

However to still allow scrolling after the view has moved up, I have registered for the keyboard notification and then allowed scrolling once the keyboard is up:

-(void) keyboardWillShow {
    [self.tableView setScrollEnabled:YES];
}


- (void)viewDidLoad {
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];

}
WINSergey
  • 1,977
  • 27
  • 39
SMSidat
  • 1,163
  • 1
  • 15
  • 34
  • Your solution seems very logical, but it doesn't work for me even if I make the tableview not scrollable in viewDidLoad() .. do you have any idea why that happens? – Arek Aug 18 '16 at 14:51
0

Thanks to @ vivek agravat Answer! Here's the swift version:

 override func viewWillAppear(_ animated: Bool) {
          super.viewWillAppear(true)
}
-1

try to scroll all your view

in viewDidLoad

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(YourVC.keyboardWillShow(_:)), name:UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(YourVC.keyboardWillHide(_:)), name:UIKeyboardWillHideNotification, object: nil)

and after

func keyboardWillShow(notification:NSNotification)
    {
        if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue()
        {
            self.view.frame.origin.y -= keyboardSize.height
        }
    }

    func keyboardWillHide(notification:NSNotification)
    {
        if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue()
        {
            self.view.frame.origin.y += keyboardSize.height
        }
    }

you can change keyboardSize

Alexander B
  • 137
  • 2
  • 12
  • You misunderstood the question. He's using a CocoaPod to handle the scrolling, and it's not the entire view that should move here -- the tableview should scroll. – BaseZen Aug 04 '16 at 15:07