Skip to content

Instantly share code, notes, and snippets.

@rahuljiresal
Last active October 30, 2015 16:34
Show Gist options
  • Save rahuljiresal/529153a63e5fb7bdae7f to your computer and use it in GitHub Desktop.
Save rahuljiresal/529153a63e5fb7bdae7f to your computer and use it in GitHub Desktop.
A small code snippet to show small in-app notifications on iOS. These notifications cover the Status Bar.
//
// StatusBarNotificationManager.h
//
// Created by Rahul Jiresal on 2015-09-23.
//
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface StatusBarNotificationManager : NSObject
+ (void)showWithMessage:(NSString*)message backgroundColor:(UIColor*)bgColor autoHide:(BOOL)autoHide;
+ (void)hide;
+ (UIColor*)errorColor;
+ (UIColor*)warningColor;
+ (UIColor*)successColor;
@end
//
// StatusBarNotificationManager.m
//
// Created by Rahul Jiresal on 2015-09-23.
//
#import "StatusBarNotificationManager.h"
@interface StatusBarNotificationManager ()
@property (strong, nonatomic) UIWindow* notificationWindow;
@property (strong, nonatomic) UILabel* notificationLabel;
@property BOOL currentlyShowing;
@end
@implementation StatusBarNotificationManager
static StatusBarNotificationManager *STATUSBARNOTIFICATIONMANAGER_SINGLETON = nil;
static bool isFirstAccess = YES;
+ (id)sharedInstance
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
isFirstAccess = NO;
STATUSBARNOTIFICATIONMANAGER_SINGLETON = [[super allocWithZone:NULL] init];
});
return STATUSBARNOTIFICATIONMANAGER_SINGLETON;
}
#pragma mark - Life Cycle
+ (id) allocWithZone:(NSZone *)zone {
return [self sharedInstance];
}
+ (id)copyWithZone:(struct _NSZone *)zone {
return [self sharedInstance];
}
+ (id)mutableCopyWithZone:(struct _NSZone *)zone {
return [self sharedInstance];
}
- (id)copy {
return [[StatusBarNotificationManager alloc] init];
}
- (id)mutableCopy {
return [[StatusBarNotificationManager alloc] init];
}
- (id) init {
if(STATUSBARNOTIFICATIONMANAGER_SINGLETON){
return STATUSBARNOTIFICATIONMANAGER_SINGLETON;
}
if (isFirstAccess) {
[self doesNotRecognizeSelector:_cmd];
}
self = [super init];
return self;
}
+ (void)showWithMessage:(NSString*)message backgroundColor:(UIColor*)bgColor autoHide:(BOOL)autoHide {
[[StatusBarNotificationManager sharedInstance] showWithMessage:message backgroundColor:bgColor];
if (autoHide) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[[StatusBarNotificationManager sharedInstance] hideAnimated:YES];
});
}
}
+ (void)hide {
[[StatusBarNotificationManager sharedInstance] hideAnimated:YES];
}
+ (UIColor*)errorColor {
return [UIColor colorWithRed:249.0/255.0 green:83.0/255.0 blue:114.0/255.0 alpha:1.0];
}
+ (UIColor*)warningColor {
return [UIColor colorWithRed:255.0/255.0 green:153.0/255.0 blue:0.0/255.0 alpha:1.0];
}
+ (UIColor*)successColor {
return [UIColor colorWithRed:29.0/255.0 green:156.0/255.0 blue:90.0/255.0 alpha:1.0];
}
- (void)showWithMessage:(NSString*)message backgroundColor:(UIColor*)bgColor {
if (!self.notificationWindow) {
self.notificationWindow = [[UIWindow alloc] initWithFrame:CGRectMake(0, -[UIApplication sharedApplication].statusBarFrame.size.height, [[UIScreen mainScreen] bounds].size.width, [UIApplication sharedApplication].statusBarFrame.size.height)];
[self.notificationWindow setWindowLevel:UIWindowLevelStatusBar + 1];
self.notificationLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [UIApplication sharedApplication].statusBarFrame.size.height)];
[self.notificationLabel setTextAlignment:NSTextAlignmentCenter];
[self.notificationLabel setTextColor:[UIColor whiteColor]];
[self.notificationLabel setFont:[UIFont systemFontOfSize:12.0]];
[self.notificationWindow addSubview:self.notificationLabel];
}
if (self.currentlyShowing) {
[self hideAnimated:NO];
}
[self.notificationWindow setBackgroundColor:bgColor];
[self.notificationLabel setText:message];
[self.notificationWindow setHidden:NO];
[UIView animateWithDuration:0.1 animations:^{
[self.notificationWindow setFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [UIApplication sharedApplication].statusBarFrame.size.height)];
}];
}
- (void)hideAnimated:(BOOL)animated {
if (animated) {
[UIView animateWithDuration:0.1 animations:^{
[self.notificationWindow setFrame:CGRectMake(0, -[UIApplication sharedApplication].statusBarFrame.size.height, [[UIScreen mainScreen] bounds].size.width, [UIApplication sharedApplication].statusBarFrame.size.height)];
} completion:^(BOOL finished) {
[self.notificationWindow setHidden:YES];
}];
}
else {
[self.notificationWindow setHidden:YES];
[self.notificationWindow setFrame:CGRectMake(0, -[UIApplication sharedApplication].statusBarFrame.size.height, [[UIScreen mainScreen] bounds].size.width, [UIApplication sharedApplication].statusBarFrame.size.height)];
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment