Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 11 getting visibleHeight and contentInset from a UITableView or UIScrollView

Pre iOS 10

if I wanted to get the visible height of a table or scroll view, I had to subtract the top and bottom inset from the height of the tableview

let tableView = ....
let height = tableView.frame.size.height - tableView.contentInset.top - tableView.contentInset.bottom

iOS 11

Unfortunately on iOS 11 suing the above method, I was am not getting the correct value.

After some debugging I realised that the top inset was 0.0, instead of the height of my navigation bar.

like image 418
zirinisp Avatar asked Sep 06 '17 15:09

zirinisp


People also ask

What is Contentinsetadjustmentbehavior?

This property specifies how the safe area insets are used to modify the content area of the scroll view. The default value of this property is UIScrollView.

What is Adjustedcontentinset?

The insets derived from the content insets and the safe area of the scroll view.

What is the content view of a uitableviewcell object?

The content view of a UITableViewCell object is the default superview for content that the cell displays. If you want to customize cells by simply adding additional views, you should add them to the content view so they position appropriately as the cell transitions in to and out of editing mode.

Should I use a scroll view or a UITableView?

For example, many developers make their life harder using a scroll view when a UITableView would be a better choice. Finally, architecture is crucial for table views. The code of the table view data source often ends inside view controllers when it should go into a separate class.

What is the default value of contentsize in UIScrollView?

The default value is 0 for top, bottom, right, and left. contentSize is the size of the content within the UIScrollView and how long it can be within scrollView. Sometimes this is dynamic like pagination or static like a contact list. This might be changing at runtime as well. This can be set programmatically as well.

What is the difference between the Settings app and UITableView?

For example, the Settings app uses tables and a navigation controller to organize the system settings. UITableView manages the basic appearance of the table, but your app provides the cells ( UITableViewCell objects) that display the actual content.


2 Answers

iOS 11 and UIScrollViewContentInsetAdjustmentBehavior

I am not getting the correct contentInset, as iOS 11 introduced UIScrollViewContentInsetAdjustmentBehavior. More information can be found on the following link:

https://developer.apple.com/documentation/uikit/uiscrollview/2902261-contentinsetadjustmentbehavior

Since UIScrollViewContentInsetAdjustmentBehavior was introduced, we have to take into account the adjustedContentInset property and add it to the contentInset.

The above code has to be updated to the following:

let visibleHeigh: CGFloat
if #available(iOS 11, *) {
  visibleHeight = tableView.frame.size.height - (tableView.contentInset.top + tableView.adjustedContentInset.top) - (tableView.contentInset.bottom + tableView.adjustedContentInset.bottom)
} else {
  visibleHeight = tableView.frame.size.height - tableView.contentInset.top - tableView.contentInset.bottom
}
like image 193
zirinisp Avatar answered Nov 16 '22 02:11

zirinisp


if you want the content inset of the scroll view to be unaffected by the "adjustedContentInset", you can disable this unpredictable behavior with the following code:

// obj-c
if (@available(iOS 11.0, *)) {
  [tableView setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];
}

// swift
if #available(iOS 11.0, *) {
  tableView.contentInsetAdjustmentBehavior = .never
}
like image 22
Lucas Chwe Avatar answered Nov 16 '22 03:11

Lucas Chwe