Disabling automatic scrolling of UITableView when

2019-01-10 14:06发布

I'm using custom UITableViewCells inside my UITableView. Each of these UITableViewCells is pretty high and contains a UITextField at the top.

When a user taps the UITextField in order to edit it, a keyboard appears and the UITableView scrolls automatically so that the cell is at the top of the screen.

The problem is that this scrolls the UITableView to the bottom of the UITableViewCell, not the top. When the UITableViewCell is high and edited the UITextField is at the top so you can't see the UITextField. I know how to scroll the UITableView programmatically, but I just don't know how to disable this automatic scrolling so that I can scroll the UITableView on my own. How can I do this?

8条回答
Luminary・发光体
2楼-- · 2019-01-10 14:33

The autoscroll-behavior is located in the UITableViewController functionality.

To disable the automatic scrolling I found two ways:

  1. Use instead of the UITableViewController simply a UIViewController - set the datasource and delegate on your own.
  2. Override the viewWillAppear method and don't call [super viewWillAppear: animated]

With both solution you disable not only the Autoscroll, but also some other nice but not essential features, that are described in the overview of Apple´s class reference:

https://developer.apple.com/documentation/uikit/uitableviewcontroller

查看更多
虎瘦雄心在
3楼-- · 2019-01-10 14:39

Did you try to set "scrollsToTop" - tableview's property to NO. By default it is YES.

查看更多
Explosion°爆炸
4楼-- · 2019-01-10 14:40

The issue for me was not so much that it scrolled but that it took the text view being edited off the screen.

So instead of preventing the scrolling, I just rescroll the tableview to where I want when the editing is triggered, like this:

public func textViewShouldBeginEditing(textView: UITextView) -> Bool {            
  dispatch_async(dispatch_get_main_queue()) {
    tableViewController.tableView.scrollToRowAtIndexPath(self.indexPath!, atScrollPosition: UITableViewScrollPosition.Middle, animated: true)
  }
  return true
}
查看更多
啃猪蹄的小仙女
5楼-- · 2019-01-10 14:46

You can try doing the following:

self.tableView.scrollEnabled = NO;

This should disable the scrollview in the tableview.

查看更多
你好瞎i
6楼-- · 2019-01-10 14:48

The best way is to subclass UITableView and then override setContentOffset(_ contentOffset: CGPoint, animated: Bool) and not to call super.setContentOffset(_ contentOffset: CGPoint, animated: Bool). In this method is where the view controller is doing the automatic scroll.

override func setContentOffset(_ contentOffset: CGPoint, animated: Bool)
{
    //Do nothing
}
查看更多
不美不萌又怎样
7楼-- · 2019-01-10 14:49

Unfortunately, overriding -viewWillAppear: doesn't work for me in iOS 8.

Here is my solution (as in UITableViewController implementation):

- (void)viewDidLoad {

[super viewDidLoad];

[[NSNotificationCenter defaultCenter] removeObserver:self.tableView name:UIKeyboardWillShowNotification object:nil];

[[NSNotificationCenter defaultCenter] removeObserver:self.tableView name:UIKeyboardWillHideNotification object:nil];
}

Since the auto-scrolling behaviour is invoked by UIKeyboard's show/hide notifications, so just NOT observe them.

查看更多
登录 后发表回答