Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unwanted UITableView reload animation when navigating from iOS 11 search controller

I have a view controller with a tableview containing a list of chats, a search controller enbeded in the navigation item (iOS 11 feature)

let searchController = UISearchController(searchResultsController: nil)
searchController.dimsBackgroundDuringPresentation = false
navigationItem.searchController = searchController
definesPresentationContext = true

When the user taps a chat in the table view the app pushes a new view controller with another table view containing messages for that chat. That works like it is supposed to:

enter image description here

The problem is that when the user activates the search controller, find some chat and taps it, the pushed view controller containing table view with the chat messages does some really strange animation with the table view that should not happen:

enter image description here

I load the data before the actual navigation and bind it to the table view in viewDidLoad using just reload() on the table view. The problematic table view uses auto layout and custom cells.

The problem is very similar to UITableView has unwanted animation when reloadData is called but for me it only happens when the iOS 11 search controller is active.

Edit: If I remove tableView.rowHeight = UITableViewAutomaticDimension and use a fixed height using func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat the problem is still there

like image 759
Igor Kulman Avatar asked Mar 08 '18 16:03

Igor Kulman


1 Answers

If you just hide the searchBar before pushing a new viewController then it may fix your problem.

You need to create a global variable for searchBarCancelButton and find the cancel button from its subviews when you search something

let buttons = searchController.searchBar.subviews.first?.subviews.filter { (view) -> Bool in
            return NSStringFromClass(view.classForCoder) == "UINavigationButton"
        } as? [UIButton]
        searchBarCancelButton = buttons?.first

then you can manually cancel it.

self.searchBarCancelButton?.sendActions(for: .touchUpInside)
like image 126
Muhammad Umair Avatar answered Oct 08 '22 17:10

Muhammad Umair