Here you can find some of the latest news from the mobile design and development world.

How-to: capture touches on the iPhone status bar

11 March

Not many users are aware of it, but if you tap the status bar when scrolling through tableviews, scrollviews and webviews, the device will automatically scroll your view all the way back to the top. This is a very convenient piece of functionality when browsing long, vertical pages, or scrolling through tableviews with a large number of rows.

But what if you wanted to use that touch to perform custom actions in your own app, would that be possible? There is no direct method to override the status bar touch behaviour, but here’s a little trick that will help you achieve the same result:

In your App delegate, you need to create a method that gets called in your [objc light=”true”]- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions[/objc] immediatelly after [objc light=”true”][self.window makeKeyAndVisible][/objc]. This method will essentially grab a reference of your keyWindow and set its windowLevel to UIWindowLevelStatusBar+1.0f.

Then you set that window background to transparent, create a new UIView using the appWindow as its frame, again set its background to clearColor and add it as a subview of your main window.

– (void)interceptStatusBarTouches
UIWindow *appWindow = [[UIApplication sharedApplication] keyWindow];
[appWindow setWindowLevel:UIWindowLevelStatusBar+1.0f];
[appWindow setBackgroundColor:[UIColor clearColor]];

StatusBarView *sb = [[StatusBarView alloc] initWithFrame:[[UIApplication sharedApplication] statusBarFrame]];
sb.backgroundColor = [UIColor clearColor];
[self.window addSubview:sb];
[sb release];

The important thing here is to actually subclass UIView so you can capture the touches received using the standard methods:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

And that’s basically all there is to it! You could pretty much do whatever you want with the touches captured with the methods above. From detecting a slide touch on the status bar and displaying a hidden view, to swapping the info that is displayed at the status bar with whatever you want.

Enjoy it!

Tags: , , , , , , , ,