Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to have message appear under + slightly above iPhone X home indicator. #111

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion CWStatusBarNotification/CWStatusBarNotification.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ typedef void(^CWCompletionBlock)(void);
* label.
*/
@interface ScrollLabel : UILabel

@property (assign, nonatomic) UIEdgeInsets edgeInsets;

/**
* Used to find the amount of time that the label will spend scrolling.
* @return The amount of time that will be spent scrolling.
Expand Down Expand Up @@ -70,7 +73,9 @@ typedef NS_ENUM(NSInteger, CWNotificationStyle) {
/// Covers the status bar portion of the screen.
CWNotificationStyleStatusBarNotification,
/// Covers the status bar and navigation bar portions of the screen.
CWNotificationStyleNavigationBarNotification
CWNotificationStyleNavigationBarNotification,
/// Appears under the home indicator and slightly above it on iPhone X models.
CWNotificationStyleHomeIndicatorNotification
};

/**
Expand Down
60 changes: 54 additions & 6 deletions CWStatusBarNotification/CWStatusBarNotification.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
#define SCROLL_SPEED 40.0f
#define SCROLL_DELAY 1.0f

#define HOMEINDICATOR_OFFSET 11.0f
#define HOMEINDICATOR_TEXT_INSET_BOTTOM 14.0f

# pragma mark - ScrollLabel

@implementation ScrollLabel
Expand Down Expand Up @@ -56,7 +59,7 @@ - (void)drawTextInRect:(CGRect)rect
if ([self scrollOffset] > 0) {
rect.size.width = [self fullWidth] + PADDING * 2;
UIGraphicsBeginImageContextWithOptions(rect.size, NO, [UIScreen mainScreen].scale);
[super drawTextInRect:rect];
[super drawTextInRect:UIEdgeInsetsInsetRect(rect, self.edgeInsets)];
textImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[textImage sizeToFit];
Expand All @@ -69,7 +72,7 @@ - (void)drawTextInRect:(CGRect)rect
}];
} else {
textImage.image = nil;
[super drawTextInRect:CGRectInset(rect, PADDING, 0)];
[super drawTextInRect:UIEdgeInsetsInsetRect(CGRectInset(rect, PADDING, 0), self.edgeInsets)];
}
}

Expand Down Expand Up @@ -238,6 +241,16 @@ - (CGFloat)getStatusBarHeight
if (self.notificationLabelHeight > 0) {
return self.notificationLabelHeight;
}
////

if (@available(iOS 11.0, *)) {
if ([self notificationStyle]==CWNotificationStyleHomeIndicatorNotification) {
return [[[UIApplication sharedApplication] keyWindow] safeAreaInsets].bottom;

}

}

CGFloat statusBarHeight = [[UIApplication sharedApplication] statusBarFrame].size.height;
if (SYSTEM_VERSION_LESS_THAN(@"8.0") && UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) {
statusBarHeight = [[UIApplication sharedApplication] statusBarFrame].size.width;
Expand All @@ -255,10 +268,23 @@ - (CGFloat)getStatusBarWidth

- (CGFloat)getStatusBarOffset
{
CGFloat offset=0.0f;

if ([self getStatusBarHeight] == 40.0f) {
return -20.0f;
offset = -20.0f;
}
////

if (@available(iOS 11.0, *)) {
if ([self notificationStyle]==CWNotificationStyleHomeIndicatorNotification) {
UIWindow *keyWindow=[[UIApplication sharedApplication] keyWindow];
offset=keyWindow.frame.size.height-[keyWindow safeAreaInsets].bottom-HOMEINDICATOR_OFFSET;

}

}
return 0.0f;

return offset;
}

- (CGFloat)getNavigationBarHeight
Expand All @@ -273,6 +299,8 @@ - (CGFloat)getNavigationBarHeight
- (CGFloat)getNotificationLabelHeight
{
switch (self.notificationStyle) {
case CWNotificationStyleHomeIndicatorNotification:
return [self getStatusBarHeight]+HOMEINDICATOR_OFFSET;
case CWNotificationStyleStatusBarNotification:
return [self getStatusBarHeight];
case CWNotificationStyleNavigationBarNotification:
Expand Down Expand Up @@ -349,6 +377,12 @@ - (void)setupNotificationView:(UIView *)view
- (void)createNotificationLabelWithMessage:(NSString *)message
{
self.notificationLabel = [ScrollLabel new];

if ([self notificationStyle]==CWNotificationStyleHomeIndicatorNotification) {
self.notificationLabel.edgeInsets=UIEdgeInsetsMake(0, 0, HOMEINDICATOR_TEXT_INSET_BOTTOM, 0);

}

self.notificationLabel.numberOfLines = self.multiline ? 0 : 1;
self.notificationLabel.text = message;
self.notificationLabel.textAlignment = NSTextAlignmentCenter;
Expand Down Expand Up @@ -480,8 +514,15 @@ - (void)displayNotificationWithMessage:(NSString *)message completion:(void (^)(
[self createNotificationLabelWithMessage:message];

// create status bar view
[self createStatusBarView];
if ([self notificationStyle]==CWNotificationStyleHomeIndicatorNotification) {
[self.statusBarView removeFromSuperview];
self.statusBarView=nil;

} else {
[self createStatusBarView];

}

// add label to window
[self.notificationWindow.rootViewController.view addSubview:self.notificationLabel];
[self.notificationWindow.rootViewController.view bringSubviewToFront:self.notificationLabel];
Expand Down Expand Up @@ -539,8 +580,15 @@ - (void)displayNotificationWithView:(UIView *)view completion:(void (^)(void))co
[self createNotificationCustomView:view];

// create status bar view
[self createStatusBarView];
if ([self notificationStyle]==CWNotificationStyleHomeIndicatorNotification) {
[self.statusBarView removeFromSuperview];
self.statusBarView=nil;

} else {
[self createStatusBarView];

}

// add view to window
UIView *rootView = self.notificationWindow.rootViewController.view;
[rootView addSubview:self.customView];
Expand Down