Safe area layout guide doesn't working for UITableViewController in storyboard

SachinVsSachin picture SachinVsSachin · Dec 14, 2017 · Viewed 9.5k times · Source

Seems like UITableViewController does not have safe area layout guide to set table view top and bottom margin as collpes with bottom layout in iPhone X, All view controller are working fine with safe area layout guide in storyboard for top and bottom margin. Except UITableView controller, please help to resolve this issue.

For reference I am attaching screenshot of iPhoneX. there you can see string "wheat" is not fitting inside of bottom safe area.

enter image description here

Answer

giorashc picture giorashc · Dec 14, 2017

I believe this happens since the view of the view controller (which always takes the entire screen size) is the tableview.

Either use a UIViewController and add a table view under its view (and add constraints to respect the bottom safe area)

or

Set the table view content insets with:

tableView.contentInset = UIEdgeInsetsMake(0, 0, UIApplication.shared.keyWindow!.safeAreaInsets.bottom, 0.0);

if you use the latter and target iOS versions under 11 make sure you verify it with:

     var safeAreaBottom: CGFloat = 0.0 
     if #available(iOS 11.0, *) {
         safeAreaBottom = UIApplication.shared.keyWindow!.safeAreaInsets.bottom
     }

     tableView.contentInset = UIEdgeInsetsMake(0, 0, safeAreaBottom, 0.0);

I also noticed this property of tableview in iOS 11 (but no description:/): insetsContentViewsToSafeArea

I didn't try it yet but you can give it a try. Maybe this is what you need out of the box