Rafael
Rafael

Reputation: 2443

How to create a scrollable view containing UIWebView with a UITableView

I have a UIView in my iphone app that contains a UIWebView and a UITableView.

The thing is, the contents have highly variable heights, so I wanted the screen to work as follows:

The problem is, both web view and table view have scroll fields of their own, and apple's documentation specifically tells me to not do what I want:

Important: You should not embed UIWebView or UITableView objects in UIScrollView objects. If you do so, unexpected behavior can result because touch events for the two objects can be mixed up and wrongly handled.

Is there a way to do what I want, or I should rethink my interface?

Upvotes: 0

Views: 643

Answers (2)

TotoroTotoro
TotoroTotoro

Reputation: 17612

This can be done, despite the Apple's warning. I've done it myself for a production app, and it worked great. I don't have the code in front of me, but I did roughly the following:

1) get the height of the web view content, and set the web view's frame to match that height.

2) do the same for the table view

The two steps above ensure that the inner scroll views won't try to scroll their content when you interact with the app. The only thing that will be able to scroll is the outer scroll view.

3) Finally, set the content size of the outer scroll view to be the sum of the web view height and the table view height.

Upvotes: 1

gWiz
gWiz

Reputation: 1284

You can add the WebView in the tableView's tableHeaderView, and adjust the height of the TableHeaderView to the total height of your web view. This way you only have a tableView that shows a WebView above it and scroll together.

Upvotes: 2

Related Questions