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.
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.
The insets derived from the content insets and the safe area of the scroll view.
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.
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.
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.
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.
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
}
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With