美文网首页
Xcode升级后iOS11及iOS13一些问题汇总

Xcode升级后iOS11及iOS13一些问题汇总

作者: 舒克5251 | 来源:发表于2020-06-08 10:27 被阅读0次

1、首先补充下创建pch文件的内容

1.1 创建pch文件
1.1.1 创建pch文件
1.1.2 改名后创建
1.1.3 pch文件创建成功
1.2 配置pch

搜索Prefix Header,路径$(SRCROOT)/xxxx


1.2.1 设置YES
1.2.2 配置路径

2、再补充下iOS11引起的一些问题

2.1 判断是否刘海屏幕

在配置好的pch中实现判断代码。
这里需要注意一下window的取值方式,最新的xcode生成工程时window在SceneDelegate中,所以在AppDelegate中会取不到。
当然,如果创建的是空模版工程,就不会有SceneDelegate了。

// 判断是否刘海屏幕
#define IsBangsScreen ({\
    BOOL isBangsScreen = NO; \
    if (@available(iOS 11.0, *)) { \
    UIWindow *window = [[UIApplication sharedApplication].windows firstObject]; \
    isBangsScreen = window.safeAreaInsets.bottom > 0; \
    } \
    isBangsScreen; \
})
2.2 使用tableView时,状态栏空出20像素

iOS11中废弃了automaticallyAdjustsScrollViewInsets,取而代之的是contentInsetAdjustmentBehavior属性,adjustedContentInset属性决定了tableView与边缘的距离。
这个问题在AppDelegate中可以统一处理。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    if (@available(iOS 11.0, *)) {
        UIScrollView.appearance.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    
    return YES;
}
2.3 使用tableView时,间隔无故变大

iOS11下tableview默认开启了self-Sizing,也就是自动估高机制。
这个问题在AppDelegate中可以统一处理。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    if (@available(iOS 11.0, *)) {
        UITableView.appearance.estimatedRowHeight = 0;
        UITableView.appearance.estimatedSectionHeaderHeight = 0;
        UITableView.appearance.estimatedSectionFooterHeight = 0;
    }
    
    return YES;
}

3、iOS13引起的一些问题

3.1 低系统跑起来黑屏的问题

简单粗暴,在AppDelegate.m中加代码即可

@synthesize window = _window;
3.2 关闭暗黑模式

在维护老工程时,为了节省成本,同时又不影响使用,有时会屏蔽暗黑模式。
全局关闭暗黑模式:在plist中新增配置User Interface Style即可,值设置为Light。


全局关闭暗黑模式

单页面关闭暗黑模式:设置overrideUserInterfaceStyle即可,值设置为UIUserInterfaceStyleLight。

if (@available(iOS 13.0, *)) {
    self.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;
} else {
    // Fallback on earlier versions
}
3.3 模态present样式改变

在维护老工程时,为了节省成本,同时又不影响使用,有时会改变新的模态present样式。
全局改变模态present样式:创建个UIViewController的类别,引一下头文件#import <objc/runtime.h>

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        SEL sel = @selector(modalPresentationStyle);
        SEL swizzSel = @selector(swiz_modalPresentationStyle);
        Method method = class_getInstanceMethod([self class], sel);
        Method swizzMethod = class_getInstanceMethod([self class], swizzSel);
        BOOL isAdd = class_addMethod(self, sel, method_getImplementation(swizzMethod), method_getTypeEncoding(swizzMethod));
        if (isAdd) {
            class_replaceMethod(self, swizzSel, method_getImplementation(method), method_getTypeEncoding(method));
        }else {
            method_exchangeImplementations(method, swizzMethod);
        }
    });
}

- (UIModalPresentationStyle)swiz_modalPresentationStyle {
    if (@available(iOS 13.0, *)){
        return UIModalPresentationFullScreen;
    }
}

单页面改变模态present样式:设置modalPresentationStyle即可。

vc.modalPresentationStyle = 0;

另外这里再说下遇到的疑难杂症
手里有一个很老的项目,代码经过很多人的手,所以没有一套完整的代码规范。
之前的Controller基本都是单独写的,没有父类,涉及这个问题的文件有上百个之多,所以设置modalPresentationStyle时,才会选择类别处理。(所以说父类很重要!!!)
当项目需要用到UIDocumentInteractionController时,会出现问题。
最开始的时候跑起来,在iOS12系统上UIDocumentInteractionController的效果是黑色背调,基本就是废废,找了很久,才想到这里可能会影响到,结果注释掉这部分代码后,发现果然好用。
问题确定后,也试了很多方法,这里就不说具体的调试了~
最后,找到一个相对来说能解决的方法,但是效果是iOS12的手机正常显示效果,iOS13的手机就是充满屏幕的效果,已经没有精力再研究了,如果大家有更好的解决方法,文章回复,互相学习~

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        SEL sel = @selector(modalPresentationStyle);
        SEL swizzSel = @selector(swiz_modalPresentationStyle);
        Method method = class_getInstanceMethod([self class], sel);
        Method swizzMethod = class_getInstanceMethod([self class], swizzSel);
        BOOL isAdd = class_addMethod(self, sel, method_getImplementation(swizzMethod), method_getTypeEncoding(swizzMethod));
        if (isAdd) {
            class_replaceMethod(self, swizzSel, method_getImplementation(method), method_getTypeEncoding(method));
        }else {
            method_exchangeImplementations(method, swizzMethod);
        }
    });
}

- (UIModalPresentationStyle)swiz_modalPresentationStyle {
    if (@available(iOS 13.0, *)){
        return UIModalPresentationFullScreen;
    }else {
        return UIModalPresentationPopover;
    }
}
@end
3.4 UISearchBar中获取UITextField变化

这是因为KVC不允许使用私有方法,直接看代码吧。

UITextField *searchField;
if (@available(iOS 13.0, *)) {
    searchField = _searchBar.searchTextField;
}else {
    // Fallback on earlier versions
    UITextField *searchTextField = [_searchBar valueForKey:@"_searchField"];
    searchField = searchTextField;
}
searchField.attributedPlaceholder = [[NSMutableAttributedString alloc] initWithString:@" " attributes:@{NSForegroundColorAttributeName: HEX_COLOR(0x666666)}];
3.5 蓝牙需要申请权限了

在info.plist中增加NSBluetoothAlwaysUsageDescription


蓝牙申请
相关的内容放到Demo里下载(这个里面主要是生成excel并导出的功能)

Demo下载地址:https://github.com/ShuKeHong/ExcelDemo.git

结语

开发语言在不断进步,身为开发者,要持续努力呀!
~~ 劝君努力,势必成功! ~~

相关文章

网友评论

      本文标题:Xcode升级后iOS11及iOS13一些问题汇总

      本文链接:https://www.haomeiwen.com/subject/ivuvzhtx.html