7

I am using both UITableViewController and UITableView in one project.

An UITableView in an UITableViewController overlays the home indicator on iPhone X. But an UITableView in an UIViewController doesn't overlay the home indicator on iPhone X. Should I fit one? And which one is correct when I consider about safe area?

e.x.

UITableViewController

UITableView on UIViewController

3 Answers 3

4

You can continue to use a UITableView on a standard UIViewController. Just set auto layout so the bottom of the tableview is flush with the bottom of the superview (and not the margin). Then set the insetsContentViewsToSafeArea property of the tableview to true

Swift:

if #available(iOS 11.0, *) {
    tableView.insetsContentViewsToSafeArea = true;
}

Objective-C:

if (@available(iOS 11.0, *)) {
    [tableView setInsetsContentViewsToSafeArea:YES];
}
1
  • 1
    If you have set UIScrollView.appearance().contentInsetAdjustmentBehavior to .never in appDelegate, remember to set the contentInsetAdjustmentBehavior property of tableView to .automatic/.always/.scrollableAxes, otherwise, the content at the bottom would be obstructed. In iOS 11.1 and earlier versions, we set it to .never to prevent an insets-adjusting animation on popping, but now(iOS 11.2) I try not setting it at the beginning, and the app works correctly. So, I think the unexpected animation in the previous version might be a bug or the handling procedures have changed.
    – LYM
    Dec 26, 2017 at 9:22
2

This is because table view in UITableViewController is root view. It will extend to full screen. But you can make constraints to bottom layout guide for table view in UIViewController. So you can use UIViewController. Or you can set content insets in UITableViewController.

0
2

This is what worked for me, in iOS 12:

I tried setting insetsContentViewsToSafeArea but that didn't work, although it set me on the right path. What fixed it for me was setting the "Content Insets" in the nib/storyboard to "Never" (in the code, that property is contentInsetAdjustmentBehavior).

Explanation: It seems that, for iPhoneX and above, the system automatically adds some bottom padding for table views so that the content goes above the home indicator.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.