views:

461

answers:

2

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?

A: 

First, you could create a custom view for the titleView of your UINavigationItem that handles the touch events.

Second, I'm a little more hazy on, but it would appear that getting a UIWebView to scroll to the top of its contents is a bit harder than with a UITableView (UIWebView does not inherit from UIScrollView). There has been some discussion about it in this question http://stackoverflow.com/questions/1361614/iphone-os-tap-status-bar-to-scroll-to-top-doesnt-work-after-remove-add-back. However, the proposed solution:

((UIScrollView *)[[webView valueForKey:@"_internal"] valueForKey:@"scroller"]).scrollsToTop = NO;

may violate the new static checking that Apple is doing on apps.

lyonanderson
Highly possibility that the could reject your app for using the private API's
nduplessis
Agreed. You almost certainly would. However, I think the first part of my answer is ok.
lyonanderson
A: 

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
Actually that I knew, what I'm mainly struggling with is how I can make it do that when I tap the UINavigationBar?
mofle
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
Alternatively you could add a UINavigationBar category which overrides the touches methods to integrate your custom code to talk to the webView
nduplessis
How exactly would I do that? :)
mofle