Scroll a UIWebView Scroll a UIWebView

I struggled with this one for quite a while. I kept searching and searching, all I could findwas forums and no one seemed to have an answer!

Well, if you're here, don't worry, I have the answer you have been looking. Let me show you how to make a UIWebView scrollable for your iPhone application.

The example below, is placed in your viewDidLoad function. In the case below, I have created the objects without the use of the Interface Builder.

The first thing we need to do is create a CGRect the size of our view:

CGRect bounds = [ [ UIScreen mainScreen ] applicationFrame ];

The next thing to do is to create our UIScrollView:

UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:bounds];

We've initialized our scrollview the size of our entire view. Now, we need to create our webview:

UIWebView *webView = [ [ UIWebView alloc ] initWithFrame:[scrollView bounds]];

Our webView has now been initialized with the bounds of our scrollView. The next thing we do is load our content from our webpage:

NSURLRequest *request = [ NSURLRequest requestWithURL:url ];

[ webView loadRequest: request ];

The following code assumes we've created an NSUrl called url with the website we want to load. Once we've loaded our webpage, we need to tell our scrollview the size of our content. In this case, we set it to the size of our webview that we've loaded:

scrollView.contentSize = [webView bounds].size;

Next, we need to add our webview to our scrollview:

[scrollView addSubview:webView];

Finally, we need to set our view as the scrollview:

self.view = scrollView;

That's it, now your webview is scrollable!

Published on Apr 4, 2009

Tags: iPhone | UIWebView

Related Posts

Did you enjoy this article? If you did here are some more articles that I thought you will enjoy as they are very similar to the article that you just finished reading.


Learn how to code in HTML, CSS, JavaScript, Python, Ruby, PHP, Java, C#, SQL, and more.

No matter the programming language you're looking to learn, I've hopefully compiled an incredible set of tutorials for you to learn; whether you are beginner or an expert, there is something for everyone to learn. Each topic I go in-depth and provide many examples throughout. I can't wait for you to dig in and improve your skillset with any of the tutorials below.