UITableView contentOffSet is not working properly

前端 未结 15 1827
滥情空心
滥情空心 2020-12-14 01:48

In viewWillAppear, I have added UISearchBar as my headerview of UITableView. When view loads, I hides UISearchbar under <

15条回答
  •  北荒
    北荒 (楼主)
    2020-12-14 02:26

    In earlier versions of the iOS SDK, methods such as viewWillAppear ran on the main thread, they now run on a background thread which is why the issue now occurs.

    Most callbacks tend to fire on a background thread so always check for thread safety when doing UI calls.

    Dispatch the change to the content offset on the main thread:

    Objective C

    dispatch_async(dispatch_get_main_queue(), ^{
        CGPoint offset = CGPointMake(0, self.searchBar.bounds.height)
        [self.tableView setContentOffset:offset animated:NO];
    });
    

    Swift 3

    DispatchQueue.main.async {
                let offset = CGPoint.init(x: 0, y: self.searchBar.bounds.height)
                self.tableView.setContentOffset(offset, animated: false)
            }
    

提交回复
热议问题