Skip to content

Commit

Permalink
Base layout on safeAreaInset instead of deprecated statusBarFrame's
Browse files Browse the repository at this point in the history
Fixes #122
Fixes #130
  • Loading branch information
calimarkus committed Oct 11, 2023
1 parent 57bab11 commit 9339f03
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@
7E5402DD286708E70079C579 /* JDSBNotificationViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 7EBE340F2844CA2D0096CD55 /* JDSBNotificationViewController.m */; };
7E5402DE286708E70079C579 /* JDSBNotificationWindow.h in Sources */ = {isa = PBXBuildFile; fileRef = 7ED140902847557600593D6F /* JDSBNotificationWindow.h */; };
7E5402DF286708E70079C579 /* JDSBNotificationWindow.m in Sources */ = {isa = PBXBuildFile; fileRef = 7ED140912847557600593D6F /* JDSBNotificationWindow.m */; };
7E5402E0286708E70079C579 /* JDSystemStatusBarHelpers.h in Sources */ = {isa = PBXBuildFile; fileRef = 7EE516D6284711C900685DDF /* JDSystemStatusBarHelpers.h */; };
7E5402E1286708E70079C579 /* JDSystemStatusBarHelpers.m in Sources */ = {isa = PBXBuildFile; fileRef = 7EE516D5284711C900685DDF /* JDSystemStatusBarHelpers.m */; };
7E5402E2286708E70079C579 /* UIApplication+JDSB_MainWindow.h in Sources */ = {isa = PBXBuildFile; fileRef = 7EBE34112844CA2D0096CD55 /* UIApplication+JDSB_MainWindow.h */; };
7E5402E3286708E70079C579 /* UIApplication+JDSB_MainWindow.m in Sources */ = {isa = PBXBuildFile; fileRef = 7EBE340D2844CA2D0096CD55 /* UIApplication+JDSB_MainWindow.m */; };
7E5402E5286709560079C579 /* JDStatusBarNotificationPresenter.h in Headers */ = {isa = PBXBuildFile; fileRef = 7E8C519428585BE400C7C003 /* JDStatusBarNotificationPresenter.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand All @@ -63,7 +61,6 @@
7E8C51A02858857200C7C003 /* TextStyleEditorView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7E8C519F2858857200C7C003 /* TextStyleEditorView.swift */; };
7E8C51A12858857200C7C003 /* TextStyleEditorView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7E8C519F2858857200C7C003 /* TextStyleEditorView.swift */; };
7E8C51A22858857200C7C003 /* TextStyleEditorView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7E8C519F2858857200C7C003 /* TextStyleEditorView.swift */; };
7EA91CAA284EF3AF00F32F09 /* JDSystemStatusBarHelpers.m in Sources */ = {isa = PBXBuildFile; fileRef = 7EE516D5284711C900685DDF /* JDSystemStatusBarHelpers.m */; };
7EA91CAB284EF3AF00F32F09 /* JDSBNotificationWindow.m in Sources */ = {isa = PBXBuildFile; fileRef = 7ED140912847557600593D6F /* JDSBNotificationWindow.m */; };
7EA91CAD284EF3AF00F32F09 /* JDSBNotificationView.m in Sources */ = {isa = PBXBuildFile; fileRef = D25B0FA4184F343600B8174B /* JDSBNotificationView.m */; };
7EA91CAF284EF3AF00F32F09 /* JDSBNotificationStyleCache.m in Sources */ = {isa = PBXBuildFile; fileRef = 7ED140962848513800593D6F /* JDSBNotificationStyleCache.m */; };
Expand All @@ -86,8 +83,6 @@
7ED140932847557600593D6F /* JDSBNotificationWindow.m in Sources */ = {isa = PBXBuildFile; fileRef = 7ED140912847557600593D6F /* JDSBNotificationWindow.m */; };
7ED140972848513800593D6F /* JDSBNotificationStyleCache.m in Sources */ = {isa = PBXBuildFile; fileRef = 7ED140962848513800593D6F /* JDSBNotificationStyleCache.m */; };
7ED140982848513800593D6F /* JDSBNotificationStyleCache.m in Sources */ = {isa = PBXBuildFile; fileRef = 7ED140962848513800593D6F /* JDSBNotificationStyleCache.m */; };
7EE516D7284711C900685DDF /* JDSystemStatusBarHelpers.m in Sources */ = {isa = PBXBuildFile; fileRef = 7EE516D5284711C900685DDF /* JDSystemStatusBarHelpers.m */; };
7EE516D8284711C900685DDF /* JDSystemStatusBarHelpers.m in Sources */ = {isa = PBXBuildFile; fileRef = 7EE516D5284711C900685DDF /* JDSystemStatusBarHelpers.m */; };
7EEDBE142856EDD9007747AA /* StatusBarPreviewView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7EEDBE132856EDD9007747AA /* StatusBarPreviewView.swift */; };
7EEDBE152856EDD9007747AA /* StatusBarPreviewView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7EEDBE132856EDD9007747AA /* StatusBarPreviewView.swift */; };
7EEDBE162856EDD9007747AA /* StatusBarPreviewView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7EEDBE132856EDD9007747AA /* StatusBarPreviewView.swift */; };
Expand Down Expand Up @@ -142,8 +137,6 @@
7ED140912847557600593D6F /* JDSBNotificationWindow.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = JDSBNotificationWindow.m; sourceTree = "<group>"; };
7ED140952848513800593D6F /* JDSBNotificationStyleCache.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = JDSBNotificationStyleCache.h; sourceTree = "<group>"; };
7ED140962848513800593D6F /* JDSBNotificationStyleCache.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = JDSBNotificationStyleCache.m; sourceTree = "<group>"; };
7EE516D5284711C900685DDF /* JDSystemStatusBarHelpers.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = JDSystemStatusBarHelpers.m; sourceTree = "<group>"; };
7EE516D6284711C900685DDF /* JDSystemStatusBarHelpers.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JDSystemStatusBarHelpers.h; sourceTree = "<group>"; };
7EEDBE132856EDD9007747AA /* StatusBarPreviewView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatusBarPreviewView.swift; sourceTree = "<group>"; };
7EFD77A12843461D000BFBF1 /* JDSBN_WindowSceneExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = JDSBN_WindowSceneExample.app; sourceTree = BUILT_PRODUCTS_DIR; };
7EFD77A32843462F000BFBF1 /* WindowScene-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "WindowScene-Info.plist"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -258,8 +251,6 @@
7EBE340F2844CA2D0096CD55 /* JDSBNotificationViewController.m */,
7ED140902847557600593D6F /* JDSBNotificationWindow.h */,
7ED140912847557600593D6F /* JDSBNotificationWindow.m */,
7EE516D6284711C900685DDF /* JDSystemStatusBarHelpers.h */,
7EE516D5284711C900685DDF /* JDSystemStatusBarHelpers.m */,
7EBE34112844CA2D0096CD55 /* UIApplication+JDSB_MainWindow.h */,
7EBE340D2844CA2D0096CD55 /* UIApplication+JDSB_MainWindow.m */,
);
Expand Down Expand Up @@ -528,8 +519,6 @@
7E5402DD286708E70079C579 /* JDSBNotificationViewController.m in Sources */,
7E5402DE286708E70079C579 /* JDSBNotificationWindow.h in Sources */,
7E5402DF286708E70079C579 /* JDSBNotificationWindow.m in Sources */,
7E5402E0286708E70079C579 /* JDSystemStatusBarHelpers.h in Sources */,
7E5402E1286708E70079C579 /* JDSystemStatusBarHelpers.m in Sources */,
7E5402E2286708E70079C579 /* UIApplication+JDSB_MainWindow.h in Sources */,
7E5402E3286708E70079C579 /* UIApplication+JDSB_MainWindow.m in Sources */,
7E5402C6286708850079C579 /* JDStatusBarNotification.docc in Sources */,
Expand All @@ -540,7 +529,6 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
7EA91CAA284EF3AF00F32F09 /* JDSystemStatusBarHelpers.m in Sources */,
7EA91CAB284EF3AF00F32F09 /* JDSBNotificationWindow.m in Sources */,
7E0FCB77285DAEB200E7CB78 /* FontPickerView.swift in Sources */,
7E2F3BBD284F6144002B2181 /* ObservableCustomStyle.swift in Sources */,
Expand Down Expand Up @@ -568,7 +556,6 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
7EE516D8284711C900685DDF /* JDSystemStatusBarHelpers.m in Sources */,
7ED140932847557600593D6F /* JDSBNotificationWindow.m in Sources */,
7E0FCB76285DAEB200E7CB78 /* FontPickerView.swift in Sources */,
7E2F3BBC284F6144002B2181 /* ObservableCustomStyle.swift in Sources */,
Expand Down Expand Up @@ -613,7 +600,6 @@
7E2F3BBB284F6144002B2181 /* ObservableCustomStyle.swift in Sources */,
7E8C51A02858857200C7C003 /* TextStyleEditorView.swift in Sources */,
7E8C519928585BE400C7C003 /* JDStatusBarNotificationStyle.m in Sources */,
7EE516D7284711C900685DDF /* JDSystemStatusBarHelpers.m in Sources */,
7ED140922847557600593D6F /* JDSBNotificationWindow.m in Sources */,
7EBE34122844CA2D0096CD55 /* UIApplication+JDSB_MainWindow.m in Sources */,
7EBE34162844CA2D0096CD55 /* JDSBNotificationViewController.m in Sources */,
Expand Down
14 changes: 4 additions & 10 deletions JDStatusBarNotification/Private/JDSBNotificationView.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#import "JDSBNotificationView.h"

#import "JDStatusBarNotificationStyle.h"
#import "JDSystemStatusBarHelpers.h"

static const NSInteger kExpectedSubviewTag = 12321;

Expand Down Expand Up @@ -352,13 +351,8 @@ - (void)setPillStyle:(JDStatusBarNotificationPillStyle *)pillStyle {

#pragma mark - Layout

static CGRect contentRectForWindow(UIView *view) {
CGFloat topLayoutMargins = view.superview.layoutMargins.top;
if (topLayoutMargins <= 8) {
// ignore default margins, fallback to system status bar height
topLayoutMargins = JDStatusBarFrameForWindowScene(view.window.windowScene).size.height;
}

static CGRect contentRectForViewMinusSafeAreaInsets(UIView *view) {
CGFloat topLayoutMargins = view.window.safeAreaInsets.top;
CGFloat height = view.bounds.size.height - topLayoutMargins;
return CGRectMake(0, topLayoutMargins, view.bounds.size.width, height);
}
Expand Down Expand Up @@ -404,11 +398,11 @@ - (void)layoutSubviews {
// content & pill view
switch (_style.backgroundStyle.backgroundType) {
case JDStatusBarNotificationBackgroundTypeFullWidth: {
_contentView.frame = contentRectForWindow(self);
_contentView.frame = contentRectForViewMinusSafeAreaInsets(self);
break;
}
case JDStatusBarNotificationBackgroundTypePill: {
_contentView.frame = [self pillContentRectForContentRect:contentRectForWindow(self)];
_contentView.frame = [self pillContentRectForContentRect:contentRectForViewMinusSafeAreaInsets(self)];
_pillView.frame = _contentView.bounds;

// setup rounded corners (not using a mask layer, so that we can use shadows on this view)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ typedef void (^ _Nullable JDSBNotificationViewControllerCompletion)(void) NS_SWI

NS_SWIFT_NAME(_SBNotificationViewControllerDelegate)
@protocol JDSBNotificationViewControllerDelegate
- (void)animationsForViewTransitionToSize:(CGSize)size;
- (void)relayoutWindowAndStatusBarView;
- (void)didDismissStatusBar;
- (void)didUpdateStyle;
@end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIVi
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];

[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
[self.delegate animationsForViewTransitionToSize:size];
[self.delegate relayoutWindowAndStatusBarView];
} completion:^(id<UIViewControllerTransitionCoordinatorContext> _Nonnull context) {
//
}];
Expand Down
37 changes: 13 additions & 24 deletions JDStatusBarNotification/Private/JDSBNotificationWindow.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#import "JDSBNotificationView.h"
#import "JDStatusBarNotificationStyle.h"
#import "UIApplication+JDSB_MainWindow.h"
#import "JDSystemStatusBarHelpers.h"

@interface JDSBNotificationWindow () <JDSBNotificationViewControllerDelegate>
@end
Expand Down Expand Up @@ -42,42 +41,37 @@ - (instancetype)initWithStyle:(JDStatusBarNotificationStyle *)style

#pragma mark - Sizing

- (void)updateFramesForStatusBarFrame:(CGRect)rect {
- (void)relayoutWindowAndStatusBarView {
// match main window transform & frame
UIWindow *window = [[UIApplication sharedApplication] jdsb_mainApplicationWindowIgnoringWindow:self];
self.transform = window.transform;
self.frame = window.frame;

// default to window width
if (CGRectIsEmpty(rect)) {
rect = CGRectMake(0, 0, window.frame.size.width, 0.0);
}

// update top bar frame
JDSBNotificationView *statusBarView = _statusBarViewController.statusBarView;
CGFloat heightIncludingNavBar = rect.size.height + contentHeight(window.windowScene, statusBarView.style, rect);
// resize statusBarView
JDSBNotificationView *const statusBarView = _statusBarViewController.statusBarView;
const CGFloat safeAreaInset = self.safeAreaInsets.top;
const CGFloat heightIncludingNavBar = safeAreaInset + contentHeight(statusBarView.style, safeAreaInset);
statusBarView.transform = CGAffineTransformIdentity;
statusBarView.frame = CGRectMake(0, 0, rect.size.width, heightIncludingNavBar);
statusBarView.frame = CGRectMake(0, 0, window.frame.size.width, heightIncludingNavBar);

// relayout progress bar
[statusBarView setProgressBarPercentage:_statusBarViewController.statusBarView.progressBarPercentage];
}

static CGFloat contentHeight(UIWindowScene *windowScene, JDStatusBarNotificationStyle *style, CGRect statusBarRect) {
static CGFloat contentHeight(JDStatusBarNotificationStyle *style, CGFloat safeAreaInset) {
switch (style.backgroundStyle.backgroundType) {
case JDStatusBarNotificationBackgroundTypeFullWidth: {
if (([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) &&
UIInterfaceOrientationIsLandscape(JDStatusBarOrientationForWindowScene(windowScene))) {
return 32.0; // match navbar height
if (safeAreaInset >= 54.0) {
return 38.66; // for dynamic island devices, this ensures the navbar separator stays visible
} else {
return 44.0; // match navbar height
return 44.0; // default navbar height
}
}
case JDStatusBarNotificationBackgroundTypePill: {
CGFloat notchAdjustment = 0.0;
if (statusBarRect.size.height >= 54.0) {
if (safeAreaInset >= 54.0) {
notchAdjustment = 0.0; // for the dynamic island, utilize the default positioning
} else if (statusBarRect.size.height > 20.0) {
} else if (safeAreaInset > 20.0) {
notchAdjustment = -7.0; // this matches the positioning of a similar system notification
} else {
notchAdjustment = 3.0; // for no-notch devices, default to a minimum spacing
Expand All @@ -89,17 +83,12 @@ static CGFloat contentHeight(UIWindowScene *windowScene, JDStatusBarNotification

#pragma mark - JDSBNotificationViewControllerDelegate

- (void)animationsForViewTransitionToSize:(CGSize)size {
// update window & statusbar
[self updateFramesForStatusBarFrame:CGRectMake(0, 0, size.width, JDStatusBarFrameForWindowScene(self.windowScene).size.height)];
}

- (void)didDismissStatusBar {
[self.delegate didDismissStatusBar];
}

- (void)didUpdateStyle {
[self updateFramesForStatusBarFrame:JDStatusBarFrameForWindowScene(self.windowScene)];
[self relayoutWindowAndStatusBarView];
}

#pragma mark - HitTest
Expand Down
8 changes: 0 additions & 8 deletions JDStatusBarNotification/Private/JDSystemStatusBarHelpers.h

This file was deleted.

25 changes: 0 additions & 25 deletions JDStatusBarNotification/Private/JDSystemStatusBarHelpers.m

This file was deleted.

0 comments on commit 9339f03

Please sign in to comment.