Making a UITableView scroll when text field is sel

2018-12-31 23:06发布

After a lot of trial and error, I'm giving up and asking the question. I've seen a lot of people with similar problems but can't get all the answers to work right.

I have a UITableView which is composed of custom cells. The cells are made of 5 text fields next to each other (sort of like a grid).

When I try to scroll and edit the cells at the bottom of the UITableView, I can't manage to get my cells properly positioned above the keyboard.

I have seen many answers talking about changing view sizes,etc... but none of them has worked nicely so far.

Could anybody clarify the "right" way to do this with a concrete code example?

30条回答
若你有天会懂
2楼-- · 2018-12-31 23:52

THE RIGHT ANSWER is Sam Ho's answer:

"If you use UITableViewController instead of UIViewController, it will automatically do so.".

Just make sure to connect your UITableView to the TableView property of the UITableViewController (so e.g. do not add it as a subview of the View property of the UITableViewController).

Also make sure to set the AutoresizingMask property of your UITableView to FlexibleHeight

查看更多
流年柔荑漫光年
3楼-- · 2018-12-31 23:52

I tried almost the same approach and came up with a simpler and smaller code for the same. I created a IBOutlet iTextView and associated with the UITextView in the IB.

 -(void)keyboardWillShow:(NSNotification *)notification
    {
        NSLog(@"Keyboard");
        CGRect keyFrame = [[[notification userInfo]objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue];

        [UIView beginAnimations:@"resize view" context:nil];
        [UIView setAnimationCurve:1];
        [UIView setAnimationDuration:1.0];
        CGRect frame = iTableView.frame;
        frame.size.height = frame.size.height -  keyFrame.size.height;
        iTableView.frame = frame;
        [iTableView scrollRectToVisible:frame animated:YES];
        [UIView commitAnimations];

    }
查看更多
君临天下
4楼-- · 2018-12-31 23:53

If you use UITableViewController instead of UIViewController, it will automatically do so.

查看更多
泪湿衣
5楼-- · 2018-12-31 23:53

I ran into something like your problem (I wanted a screen similar to the iPhone's settings.app with a bunch of editable cells stacked on on top of another) and found that this approach worked well:

sliding uitextfields around to avoid

查看更多
看淡一切
6楼-- · 2018-12-31 23:54

The simplest solution for Swift 3, based on Bartłomiej Semańczyk solution:

override func viewDidLoad() {
    super.viewDidLoad()

    NotificationCenter.default.addObserver(self, selector: #selector(CreateEditRitualViewController.keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardDidShow, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(CreateEditRitualViewController.keyboardWillHide(notification:)), name: NSNotification.Name.UIKeyboardDidHide, object: nil)
}

deinit {
    NotificationCenter.default.removeObserver(self)
}

// MARK: Keyboard Notifications

@objc func keyboardWillShow(notification: NSNotification) {
    if let keyboardHeight = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue.height {
        tableView.contentInset = UIEdgeInsetsMake(0, 0, keyboardHeight, 0)
    }
}

@objc func keyboardWillHide(notification: NSNotification) {
    UIView.animate(withDuration: 0.2, animations: {
        // For some reason adding inset in keyboardWillShow is animated by itself but removing is not, that's why we have to use animateWithDuration here
        self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0)
    })
}
查看更多
ら面具成の殇う
7楼-- · 2018-12-31 23:54

If you use Three20, then use the autoresizesForKeyboard property. Just set in the your view controller's -initWithNibName:bundle method

self.autoresizesForKeyboard = YES

This takes care of:

  1. Listening for keyboard notifications and adjusting the table view's frame
  2. Scrolling to the first responder

Done and done.

查看更多
登录 后发表回答