mwaterfall / MWPhotoBrowser

A simple iOS photo and video browser with grid view, captions and selections.
MIT License
8.75k stars 2.71k forks source link

Support for iPhone X safe area #618

Open calbot opened 6 years ago

calbot commented 6 years ago

Bottom toolbar gets cut off on iPhone X so here's what I did to fix it. Updated MWPhotoBrowser.m


- (CGRect)frameForToolbarAtOrientation:(UIInterfaceOrientation)orientation {
    CGFloat height = 44;
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone &&
        UIInterfaceOrientationIsLandscape(orientation)) height = 32;
    CGFloat adjust = 0;
    if (@available(iOS 11.0, *)) {
        //Account for possible notch
        UIEdgeInsets safeArea = [[UIApplication sharedApplication] keyWindow].safeAreaInsets;
        adjust = safeArea.bottom;
    }
    return CGRectIntegral(CGRectMake(0, self.view.bounds.size.height - height - adjust, self.view.bounds.size.width, height));
}
foolsparadise commented 6 years ago

如果不好使,另加一个其它方法 If unuseful, you can check this 这一块代码,在commit 66ddd73-on Apr 22, 2016基础上添加的,使用工程中的Library photos and videos一块,UI在iPhoneX上我这边试了下是OK了,其它建议类推。希望能帮到你。 Library photos and videos UI in this project , checked Run OK in iPhoneX , code based on commit 66ddd73-on Apr 22, 2016 , maybe help you

diff --git a/Pod/Classes/MWPhotoBrowser.m b/Pod/Classes/MWPhotoBrowser.m
index 0de7faa..80dcf8d 100644
--- a/Pod/Classes/MWPhotoBrowser.m
+++ b/Pod/Classes/MWPhotoBrowser.m
@@ -15,6 +15,12 @@

 #define PADDING                  10

+#define IOS11_OR_LATER_SPACE(par) ({ float space = 0.0; if ([[UIDevice currentDevice] systemVersion].floatValue>=11) { space = par; } (space); })
+#define JF_KEY_WINDOW [UIApplication sharedApplication].keyWindow
+#define JF_TOP_SPACE IOS11_OR_LATER_SPACE(JF_KEY_WINDOW.safeAreaInsets.top)
+#define JF_TOP_ACTIVE_SPACE IOS11_OR_LATER_SPACE(MAX(0, JF_KEY_WINDOW.safeAreaInsets.top-20))
+#define JF_BOTTOM_SPACE IOS11_OR_LATER_SPACE(JF_KEY_WINDOW.safeAreaInsets.bottom)
+
 static void * MWVideoPlayerObservation = &MWVideoPlayerObservation;

 @implementation MWPhotoBrowser
@@ -161,6 +167,9 @@ static void * MWVideoPlayerObservation = &MWVideoPlayerObservation;

     // Toolbar
     _toolbar = [[UIToolbar alloc] initWithFrame:[self frameForToolbarAtOrientation:self.interfaceOrientation]];
+    CGRect frame = _toolbar.frame;
+    frame.origin.y = self.view.bounds.size.height - frame.size.height - JF_BOTTOM_SPACE;
+    _toolbar.frame = frame;
     _toolbar.tintColor = [UIColor whiteColor];
     _toolbar.barTintColor = nil;
     [_toolbar setBackgroundImage:nil forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
@@ -283,6 +292,9 @@ static void * MWVideoPlayerObservation = &MWVideoPlayerObservation;
         [_toolbar removeFromSuperview];
     } else {
         [self.view addSubview:_toolbar];
+        CGRect frame = _toolbar.frame;
+        frame.origin.y = self.view.bounds.size.height - frame.size.height - JF_BOTTOM_SPACE;
+        _toolbar.frame = frame;
     }

     // Update nav
@@ -495,7 +507,9 @@ static void * MWVideoPlayerObservation = &MWVideoPlayerObservation;

        // Toolbar
        _toolbar.frame = [self frameForToolbarAtOrientation:self.interfaceOrientation];
-    
+    CGRect frame = _toolbar.frame;
+    frame.origin.y = self.view.bounds.size.height - frame.size.height - JF_BOTTOM_SPACE;
+    _toolbar.frame = frame;
        // Remember index
        NSUInteger indexPriorToLayout = _currentPageIndex;

@@ -1438,7 +1452,9 @@ static void * MWVideoPlayerObservation = &MWVideoPlayerObservation;

         // Toolbar
         _toolbar.frame = CGRectOffset([self frameForToolbarAtOrientation:self.interfaceOrientation], 0, animatonOffset);
-        
+        CGRect frame = _toolbar.frame;
+        frame.origin.y = self.view.bounds.size.height - frame.size.height - JF_BOTTOM_SPACE;
+        _toolbar.frame = frame;
         // Captions
         for (MWZoomingScrollView *page in _visiblePages) {
             if (page.captionView) {
@@ -1461,6 +1477,9 @@ static void * MWVideoPlayerObservation = &MWVideoPlayerObservation;
         // Toolbar
         _toolbar.frame = [self frameForToolbarAtOrientation:self.interfaceOrientation];
         if (hidden) _toolbar.frame = CGRectOffset(_toolbar.frame, 0, animatonOffset);
+        CGRect frame = _toolbar.frame;
+        frame.origin.y = self.view.bounds.size.height - frame.size.height - JF_BOTTOM_SPACE;
+        _toolbar.frame = frame;
         _toolbar.alpha = alpha;

         // Captions