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

需要适配"灵动岛(dynamic island)" #201

Open
vast0608 opened this issue Sep 16, 2022 · 1 comment
Open

需要适配"灵动岛(dynamic island)" #201

vast0608 opened this issue Sep 16, 2022 · 1 comment

Comments

@vast0608
Copy link

vast0608 commented Sep 16, 2022

Xcode Version:14.0 (14A309)
iOS Version:16.0
模拟器:iPhone 14 Pro、iPhone 14 Pro Max
WRNavigationBar Version:1.4.0(16 Jul 2021)

iPhone竖屏状态下,“灵动岛”状态栏高度:54pt
目前带“灵动岛”的机型,状态栏+导航栏的高度有异常

带“灵动岛”的机型,状态栏+导航栏的高度理论值为54pt+44pt=98pt
该库展示的状态栏+导航栏的高度小于98pt

还请大佬有时间适配一下

@vast0608
Copy link
Author

vast0608 commented Sep 16, 2022

@wangrui460 我先用一个临时方法兼容着,期待大佬能从源代码上适配“灵动岛”

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface UINavigationBar (dynamic_island)

@end

NS_ASSUME_NONNULL_END
#import "UINavigationBar+dynamic_island.h"
#import <objc/runtime.h>

@implementation UINavigationBar (dynamic_island)

+(void)initialize
{
    static dispatch_once_t token;
    dispatch_once(&token, ^{
        
        //backgroundViewFrame
        SEL orginSel = NSSelectorFromString(@"backgroundViewFrame");
        SEL overrideSel = @selector(newBackgroundViewFrame);
        
        Method originMethod = class_getInstanceMethod([self class], orginSel);
        Method overrideMethod = class_getInstanceMethod([self class], overrideSel);
        
        //原来的类没有实现指定的方法,那么我们就得先做判断,把方法添加进去,然后进行替换
        if (class_addMethod([self class], orginSel, method_getImplementation(overrideMethod) , method_getTypeEncoding(originMethod))) {
            class_replaceMethod([self class],
                                overrideSel,
                                method_getImplementation(originMethod),
                                method_getTypeEncoding(originMethod));
            
        }else{
            //交换实现
            method_exchangeImplementations(originMethod, overrideMethod);
        }
    });
}

- (CGRect) newBackgroundViewFrame {
    [self newBackgroundViewFrame];
    //根据真实的状态栏高度来计算
    CGRect barFrame = self.frame;
    CGFloat height = barFrame.origin.y + barFrame.size.height;
    CGRect rect = CGRectMake(0, 0, self.bounds.size.width, height);
    if (@available(iOS 16.0, *)) {
        if([UIApplication sharedApplication].statusBarFrame.size.height >= 54){//有灵动岛的机型
            rect = CGRectMake(0, 5, self.bounds.size.width, height+1);
        }
    }
    return rect;
}

@end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant