Twitter-like Notification

I really like how inapp notifications work in the latest version of Twitter for iPhone (July 2012). This approach also seems to be very easy to implement so I decided to create a very easy example showing one way how this can be done.

The trick is to add a label or whatever you want to application’s window. That’s an easy task since UIWindow is a sublass of UIView. Once you have this done, you can easily hide the native statusbar and display the label.

//
//  NotifWindow.m
//  TwitterLikeNotification
//
//  Created by Petr Pavlik on 7/11/12.
//  Copyright (c) 2012 Petr Pavlik. All rights reserved.
//

#import "NotifWindow.h"

@interface NotifWindow ()

@property(nonatomic, strong) UILabel* notificationLabel;

@end

@implementation NotifWindow

@synthesize notificationLabel = _notificationLabel;

- (void) showNotificationWithText:(NSString*)text {

    if (!self.notificationLabel) {
        self.notificationLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 20)];
        self.notificationLabel.textColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.8];
        self.notificationLabel.backgroundColor = [UIColor blackColor];
        self.notificationLabel.textAlignment = UITextAlignmentCenter;
        self.notificationLabel.font = [UIFont boldSystemFontOfSize:13.0f];
        [self addSubview:self.notificationLabel];
    }

    self.notificationLabel.text = text;

    self.notificationLabel.frame = CGRectMake(0, -20, 320, 20);

    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];

    __weak NotifWindow* weakSelf = self;

    double delayInSeconds = 0.5;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){

        [UIView animateWithDuration:0.5 animations:^{
            weakSelf.notificationLabel.frame = CGRectMake(0, 0, 320, 20);
        }];

    });

    delayInSeconds = 4.0;
    popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){

        [UIView animateWithDuration:0.5 animations:^{
            weakSelf.notificationLabel.frame = CGRectMake(0, -20, 320, 20);
        }];

    });

    delayInSeconds = 4.5;
    popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){

        [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationSlide];
    });

}

@end

And this is how it can be used.

- (IBAction)showNotification:(id)sender {

    NotifWindow* notifWindow = (NotifWindow*)self.view.window;
    [notifWindow showNotificationWithText:@"Nice!"];

}

Please note that this is a very simple example. If you want to implement such functionality into your app, you shout take care of things like handling of landscape mode or queuing of notifications.

Tags: , , , ,

Unknown's avatar

About Petr Pavlik

I'm an iOS developer currently living in Prague.

Leave a comment