Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Twitter official iOS app changes status bar with custom text

The new official Twitter for iOS app changes statusbar just like in the picture above. How to implement such a feature?

[image source]

like image 715
ahmet alp balkan Avatar asked Oct 13 '12 19:10

ahmet alp balkan


1 Answers

Here's how I do it in my app with a rotating animation:

-(void)showStatusBarMessage:(NSString *)message hideAfter:(NSTimeInterval)delay
{
    __block UIWindow *statusWindow = [[UIWindow alloc] initWithFrame:[UIApplication sharedApplication].statusBarFrame];
    statusWindow.windowLevel = UIWindowLevelStatusBar + 1;
    UILabel *label = [[UILabel alloc] initWithFrame:statusWindow.bounds];
    label.textAlignment = UITextAlignmentCenter;
    label.backgroundColor = [UIColor blackColor];
    label.textColor = [UIColor grayColor];
    label.font = [UIFont boldSystemFontOfSize:13];
    label.text = message;
    [statusWindow addSubview:label];
    [statusWindow makeKeyAndVisible];
    label.layer.transform = CATransform3DMakeRotation(M_PI * 0.5, 1, 0, 0);
    [UIView animateWithDuration:0.7 animations:^{
        label.layer.transform = CATransform3DIdentity;
    }completion:^(BOOL finished){
        double delayInSeconds = delay;
        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:^{
                label.layer.transform = CATransform3DMakeRotation(M_PI * 0.5, -1, 0, 0);
            }completion:^(BOOL finished){
                statusWindow = nil;
                [[[UIApplication sharedApplication].delegate window] makeKeyAndVisible];
            }];
        });
    }];
}

I have it in a category on UIViewController to use it from whatever controller in the app.

EDIT: Needs QuartzCore.framework to be referenced and #import <QuartzCore/QuartzCore.h> to be added.

like image 71
Moxy Avatar answered Nov 04 '22 13:11

Moxy