Steve Weller
Steve Weller

Reputation: 4619

How do I detect a touch in the iPhone status bar?

I have a UITableview that I would like to scroll to the top when the status bar is touched.

I expected to find a notification that I could listen to and then use tableview:scrollToRowAtIndexPath: for the scroll, but can find none.

Is there a way to do this?

[Update]

Finding some more on the net, I am suspicious that this is the simulator biting me. My UIScrollView is failing to scroll to the top in the simulator, but I have not tried on hardware. Also, I have one UIScrollView, but also UITextView, so I wonder if that is eating it.

I will see what I can do with scrollsToTop.

Upvotes: 2

Views: 2542

Answers (3)

Keller
Keller

Reputation: 17081

This might be major overkill, but you could write your own status bar.

You can hide the actual status bar by setting the "Status bar is initially hidden" to YES in your application plist. Then create your own custom status bar view at the top of the view with height 20px that is either a UIButton or simply a UIView with a UITapGestureRecognizer.

You can detect battery level/state (charge and plugged-in or not) with:

UIDevice *myDevice = [UIDevice currentDevice];
[myDevice setBatteryMonitoringEnabled:YES];

float batteryLevel = [myDevice batteryLevel];
UIDeviceBatteryState batteryState = [myDevice batteryState];

You can get the time with [NSDate date]; (then use NSDateFormatter to format).

Getting the signal strength is a little iffy but can be roughly estimated with a certain degree of ingenuity.

Upvotes: 0

Chris Lundie
Chris Lundie

Reputation: 6023

It should be happening automatically unless you set the scrollsToTop property to NO.

Upvotes: 1

Kriem
Kriem

Reputation: 8705

This article on cocoawithlove.com is exactly what you want.

From that article:

"The trickiest part of the sample application is detecting a touch in the status bar.

By implementing a custom setContentOffset:animated: method on a UITableView and setting a breakpoint in that method, you can see in the debugger stack that the UIApplication sendEvent: method is invoked for status bar touches, so that's where we'll begin."

Upvotes: 4

Related Questions