Search code examples
iosuitableviewautolayoutsafearealayoutguide

How to remove safe area when tableview is used?


I have simple UITableView inside UIViewController. Here is configurations and constraints I set:

    tableView.translatesAutoresizingMaskIntoConstraints = false
    tableView.insetsContentViewsToSafeArea = false
    view.addSubview(tableView)
    tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
    tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
    tableView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
    tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true

By doing this, I canceled safeArea in UITableView and set top, bottom constrains ignoring safeAreaLayoutGuide. Yes, it perfectly works when I scroll by table and safe area is ignored as I want. Here is image showing this:

enter image description here

But the problem is at the beginning when I don't start scrolling or touching table. It starts below safe area, not out of safe area. Here it is:

enter image description here

I don't want it to happen. I want my UITableView(or first UITableViewCell) from the uppermost point ignoring safe area. It seems UITableView ignores this even if I set constraints correctly. So, how to solve this problem?


Solution

  • You need to also set the tableView's contentInsetAdjustmentBehavior:

    tableView.contentInsetAdjustmentBehavior = .never