Sizing to a table view's content size
Description
Hi, first of all I wanted to thank you for taking the time to publish this library. It seems well put together and well documented. Kudos! 👊 With that out of the way, let's dive in:
My use case is that I want to wrap a view controller containing a table view in a bottom sheet, and make the bottom sheet size itself to the table view's vertical content size up until it basically fills the entire screen (minus some offset at the top for the drag handle, etc). So if the table view has a small vertical content size, the bottom sheet is small. If the table view has a large vertical content size, make it fill (almost) the entire screen and let the table view be scrollable within the bottom sheet.
I wasn't exactly sure how to achieve this, but what I've come up with is this:
So, in the view controller that has the table view, I added this (cut for brevity, and sorry for the ReactiveSwift stuff here, it's just what I like to use -- I think you will get the gist of it):
tableView.reactive.signal(for: \.contentSize)
.observeValues { _ in
// This seemed to be necessary to not end up in a deadlock
DispatchQueue.main.async {
self.panModalSetNeedsLayoutUpdate()
}
}
So basically, when the content size of the table view changes, tell the pan modal that the layout needs to be updated. I also have this extension, adding conformance to PanModalPresentable:
extension SomeTableViewViewController: PanModalPresentable {
var panScrollable: UIScrollView? { tableView }
var longFormHeight: PanModalHeight { .contentHeight(tableView.contentSize.height) }
}
This seems to be working well for my use case. But, I have two questions here:
- Is this the way you would do this yourselves?
- If this is indeed 'the way', would it be desirable to make this behavior a little easier to achieve? If so, would you be willing to accept me having a go at it if I would find the time to make PR for it?
Also -- in my use case I'm using a table view, but I guess it can be generalized to any scroll view.
Thanks.
What type of issue is this?
- [ ] bug
- [ ] enhancement (feature request)
- [x] question
- [ ] documentation related
- [ ] testing related
- [ ] discussion
Requirements
- [x] I've read and understood the Contributing guidelines and have done my best effort to follow them.
- [x] I've read and agree to the Code of Conduct.
- [x] I've searched for any related issues and avoided creating a duplicate issue.