Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Safe area layout guide not working for UITableViewController in storyboard

Seems like UITableViewController does not have safe area layout guide to set table view top and bottom margin as collapses 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.

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

like image 539
SachinVsSachin Avatar asked Jan 27 '26 08:01

SachinVsSachin


1 Answers

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

like image 97
giorashc Avatar answered Jan 29 '26 23:01

giorashc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!