0

I have a really long UIWebView, and I need to add a way for the user to tap the UINavigationBar to scroll to top (something like the Facebook app, where it's little glow when you tap).

How can I do this?

Sindre Sorhus
  • 62,972
  • 39
  • 168
  • 232

2 Answers2

2

In iOS5, you can now access the UIScrollView directly, allowing you to scroll to top.

[webView.scrollView scrollRectToVisible:animated:]

That should take care of everything that you need.

Matthew Knippen
  • 1,048
  • 9
  • 22
1

You could use javascript to scroll the web view to the top. You could execute the javascript from the Objective-C side using

- (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script
nduplessis
  • 12,236
  • 2
  • 36
  • 53
  • Actually that I knew, what I'm mainly struggling with is how I can make it do that when I tap the UINavigationBar? – Sindre Sorhus Dec 06 '09 at 20:38
  • As lyonanderson said, you could create a custom titleView for your navigation bar to handle the touch events and execute the javascript in your webView – nduplessis Dec 06 '09 at 21:51
  • Alternatively you could add a UINavigationBar category which overrides the touches methods to integrate your custom code to talk to the webView – nduplessis Dec 06 '09 at 21:54
  • How exactly would I do that? :) – Sindre Sorhus Dec 06 '09 at 22:12
  • Decent solution. The smoother way to do it using the UIWebView's internal UIScrollView and scrolling it to the top, although it's a bit hacky. – Sam Soffes Mar 31 '11 at 22:24