Unwanted UITableView reload on transition from iOS 11 Search Controller

I have a view controller with a table view containing a list of chats, a search controller included in the navigation element (iOS 11 function)

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

When a user selects a chat in a table view, the application launches a new view controller with another table view containing messages for that chat. This works as expected:

enter image description here

The problem is that when the user activates the search controller, I find some chat and dials it, then the moved view controller containing a table view with chat messages really causes a strange animation with a 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 only reload() in the table view. The problem table view uses auto-layout and custom cells.

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

Change If I remove tableView.rowHeight = UITableViewAutomaticDimension and use fixed height with func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat , the problem still exists

+7
ios uitableview autolayout swift ios11
source share
7 answers

Personally, I would simply hide the searchView controller before submitting a new view controller. (For example, using UIView.animates with a completion handler)

I would not investigate further, because with iOS11 there is an esoteric problem in managing safe areas. Beetle?:)

Even launcher screen layouts are not handled properly. So many majors logos missed their middle part at launch!

0
source share

If you just hide the searchBar before clicking on the new viewController, it may fix your problem.

You need to create a global variable for searchBarCancelButton and find the cancel button from your subzones when searching for 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) 
+4
source share

You can try calling cell.layoutIfNeeded() right after deleting and setting the contents of the cell

0
source share

iOS 11 has completely updated the safe area API, including scroll insertion customization behavior, which can cause unwanted animations when ignored. Therefore, turn off the automatic configuration of pasting content for scrolling using unwanted animations:

 if #available(iOS 11.0, *) { tableView.contentInsetAdjustmentBehavior = .never } else { // < iOS 11 logic } 
0
source share

Do not use the reloadData() method in viewDidLoad or viewWillAppear . Instead, reload the tableView with empty data in viewDidLoad so that your tableView doesn't show anything and then invoke reloadData() in your viewDidAppear method viewDidAppear all your chats. This will limit your tableView from loading unwanted animations.

 var shouldShowEmpty = true func viewDidLoad() { super.viewDidLoad() tableView.reloadData() } func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) shouldShowEmpty = false tableView.reloadData() } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if shouldShowEmpty { return 0 } return yourArray.count } 
0
source share

According to the attachment. Apparently, due to the automatic layout, the height of the cell element was zero ( UILabel or UIImageView ), and then suddenly, when the table view is reloaded, it receives data inside the cell that increases its height (automatic size or something else). what cause this animation.

Try clicking it without an animation or trying to set the height and width of a cell's cell element and check if this animation is still showing.

Did you check without calling UISearchBar, if you select on any cell, does the same animation happen? or did you try to remove the UISearchbar and select in the cell and check part of the animation?

Please share your code so that we can see more clearly.

0
source share
  • In VC2, try the delay function before reloading the table

    DispatchQueue.main.asyncAfter (deadline: .now () + 0.1) {tableView.reloadData}

or more

  • In VC1 in didSelect cancel the first search controller responder before pressing VC2

or

  • In VC1 in didSelect cancel the first search controller responder and set delay before pressing VC2.
-one
source share

All Articles