views:

255

answers:

4

I'm trying to detect any touch on the iPhone's UIStatusBar but its not working. I've tried subclassing UIApplication and UIWindow to access it but still nothing

A: 

You can't. The status bar is held by the application. You can only turn it on and off.

More importantly, any fiddling with the status bar is likely to get your app rejected by Apple because it could interfere with the operation of the hardware e.g. disguising or hiding a low battery.

If you need full screen touches for your views you should just hide the status bar.

TechZen
It *is* possible (depending on the first responder) and is done in an approved application: http://blog.instapaper.com/post/420370426
Tom
+1  A: 

You need to have a UIScrollView in your responder chain, and it needs to have a delegate set. You can then override scrollViewShouldScrollToTop: in the delegate, which will be called when the user taps on the status bar. Be sure to return NO if you don't want the default behavior of scrolling the scroll view back to the top.

Tom
+1  A: 

Based on Tom's answer, I wrote the code. It works well.

- (void)viewDidLoad {
    [super viewDidLoad];
    // required
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectZero];
    scrollView.delegate = self;
    scrollView.contentSize = CGSizeMake(0.0f,1.0f);
    [scrollView setContentOffset:CGPointMake(0.0f,1.0f) animated:NO];
    // optional
    scrollView.scrollsToTop = YES; // default is YES.
    [self.view addSubview:scrollView];
}

- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView {
    NSLog(@"Detect status bar is touched.");
    /* Your own code. */
    return NO;
}
KatokichiSoft
A: 

Tom, thanks for the tip. Unfortunately I'm doing everything you suggest but I'm still not receiving the event. The scrollView is the first responder and the ViewController is the delegate. Any other reason why you think this might not work?

After trying a number of things, I found the answer. My UIScrollView contained multiple UIWebViews. Each UIWebView contains a UIScrollView which by default has scrollsToTop == YES;The trick was to dig into the UIWebView and set the associated UIScrollView's scrollsToTop to NO. After that the original UIScrollView's scrollViewShouldScrollToTop did not get intercepted by those of the UIWebView.