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

Fix checking safe area to prevent weird iPhone X behaviour. #235

Open
wants to merge 1 commit 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
13 changes: 13 additions & 0 deletions CRToast/CRToastLayoutHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,19 @@ static CGFloat CRGetNavigationBarHeightForOrientation(UIInterfaceOrientation ori
CRNavigationBarDefaultHeightiPhoneLandscape;
}

#pragma mark - Safe area offset
/**
Get safe area offset to fix iPhone X top notch
*/
static CGFloat CRGetSafeAreaTopOffset() {
if (@available(iOS 11.0, *)) {
UIWindow *window = UIApplication.sharedApplication.keyWindow;
CGFloat topOffset = window.safeAreaInsets.top;
return topOffset;
}
return 0.0;
}

#pragma mark - Notification Frame
/**
Get the height of view needed to contain the notification given the specific orienation & notification type
Expand Down
15 changes: 8 additions & 7 deletions CRToast/CRToastView.m
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,16 @@ - (void)layoutSubviews {
CGRect contentFrame = self.bounds;
CGSize imageSize = self.imageView.image.size;
CGFloat preferredPadding = self.toast.preferredPadding;

CGFloat safeAreaOffset = CRGetSafeAreaTopOffset() / 2; // Divide by 2 to have better bottom margin
CGFloat statusBarYOffset = self.toast.displayUnderStatusBar ? (CRGetStatusBarHeight()+CRStatusBarViewUnderStatusBarYOffsetAdjustment) : 0;
contentFrame.size.height = CGRectGetHeight(contentFrame) - statusBarYOffset;

contentFrame.size.height = CGRectGetHeight(contentFrame) - statusBarYOffset - safeAreaOffset;

self.backgroundView.frame = self.bounds;

CGFloat imageXOffset = CRImageViewFrameXOffsetForAlignment(self.toast.imageAlignment, preferredPadding, contentFrame.size);
self.imageView.frame = CGRectMake(imageXOffset,
statusBarYOffset,
statusBarYOffset + safeAreaOffset,
imageSize.width == 0 ?
0 :
CGRectGetHeight(contentFrame),
Expand All @@ -152,7 +153,7 @@ - (void)layoutSubviews {
if (self.toast.showActivityIndicator) {
CGFloat centerX = CRCenterXForActivityIndicatorWithAlignment(self.toast.activityViewAlignment, CGRectGetHeight(contentFrame), CGRectGetWidth(contentFrame), preferredPadding);
self.activityIndicator.center = CGPointMake(centerX,
CGRectGetMidY(contentFrame) + statusBarYOffset);
CGRectGetMidY(contentFrame) + statusBarYOffset + safeAreaOffset);

[self.activityIndicator startAnimating];
x = MAX(CRContentXOffsetForViewAlignmentAndWidth(self.toast.activityViewAlignment, imageXOffset, CGRectGetHeight(contentFrame), preferredPadding), x);
Expand All @@ -172,7 +173,7 @@ - (void)layoutSubviews {

if (self.toast.subtitleText == nil) {
self.label.frame = CGRectMake(x,
statusBarYOffset,
statusBarYOffset + safeAreaOffset,
width,
CGRectGetHeight(contentFrame));
} else {
Expand All @@ -191,13 +192,13 @@ - (void)layoutSubviews {
CGFloat offset = (CGRectGetHeight(contentFrame) - (height + subtitleHeight))/2;

self.label.frame = CGRectMake(x,
offset+statusBarYOffset,
offset+statusBarYOffset+safeAreaOffset,
CGRectGetWidth(contentFrame)-x-kCRStatusBarViewNoImageRightContentInset,
height);


self.subtitleLabel.frame = CGRectMake(x,
height+offset+statusBarYOffset,
height+offset+statusBarYOffset+safeAreaOffset,
CGRectGetWidth(contentFrame)-x-kCRStatusBarViewNoImageRightContentInset,
subtitleHeight);
}
Expand Down