0

Xcode 11.1, iOS 13.1

I'm having difficulty figuring out why the TableView cells end up behind the search bar.

My setup:

override func viewDidLoad() {
    // Setup Eureka's UITableViewStyle to `Plain`  (like 'Contacts' app)
    // https://github.com/xmartlabs/Eureka/issues/218
    if tableView == nil {
        tableView = UITableView(frame: view.bounds, style: UITableView.Style.plain)
        tableView?.autoresizingMask = UIView.AutoresizingMask.flexibleWidth.union(.flexibleHeight)
        tableView.cellLayoutMarginsFollowReadableWidth = false
    }
    // *now* call super.
    super.viewDidLoad()

    // Add search bar
    searchController.searchResultsUpdater = self
    searchController.obscuresBackgroundDuringPresentation = false
    definesPresentationContext = true

    // Place the search bar in the navigation bar.
    self.navigationItem.searchController = self.searchController
    self.navigationItem.hidesSearchBarWhenScrolling = false
}

enter image description here

Sebastian Dwornik
  • 2,526
  • 2
  • 34
  • 57

2 Answers2

0

Add the following constraints in after tableView = UITableView(frame: view.bounds, style: UITableView.Style.plain)

tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 0).isActive = true
tableView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 0).isActive = true
tableView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: 0).isActive = true
tableView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: 0).isActive = true
ChrisTheGreat
  • 512
  • 4
  • 21
  • `*** Terminating app due to uncaught exception 'NSGenericException', reason: 'Unable to activate constraint with anchors and because they have no common ancestor. Does the constraint or its anchors reference items in different view hierarchies? That's illegal.'` – Sebastian Dwornik Oct 17 '19 at 12:09
  • I switched to using: `tableView.tableHeaderView = searchController.searchBar`, and added your code within `viewWillLayoutSubviews`. Placing the search bar in the navigation bar became too problematic. – Sebastian Dwornik Oct 17 '19 at 14:53
  • `navigationItem.searchController = searchController` should work. – ChrisTheGreat Oct 18 '19 at 07:19
0

After countless more testing on iOS 12 and 13, because I was getting different UX results, I discovered this: https://stackoverflow.com/a/57861125/7599

Which results in: (using Snapkit)

extendedLayoutIncludesOpaqueBars = true

    self.tableView.snp.remakeConstraints { make -> Void in
        make.top.equalToSuperview()
        make.leading.equalTo(self.view.safeAreaLayoutGuide.snp.leading)
        make.trailing.equalTo(self.view.safeAreaLayoutGuide.snp.trailing)
        make.bottom.equalToSuperview()
    }
Sebastian Dwornik
  • 2,526
  • 2
  • 34
  • 57