iOS13Xcode11趟坑(适配)

目录
.
1.presentViewController出来的控制器默认不全屏(变成卡片式了)
.
2.关于访问修改私有变量造成的闪退
.
3.如何让夜间模式(深色模式)不影响你的程序(如何关闭深色模式)
.
4.iOS 13的tabbar设置问题(tabbar的顶部横线设置失效/tabBarItem颜色失效)
.
5.Xcode11 缺失库文件导入libstdc-6.0.9
.
6.Xcode11新增SceneDelegate后如何更改window的根控制器, 并兼容iOS13以下的版本
6.1 使用SceneDelegate管理生命周期的风险: 使用UIAlertView闪退
6.2 iOS13不使用SceneDelegate

1.presentViewController出来的控制器默认不全屏(变成卡片式了)

原因:控制器的 modalPresentationStyle 属性的默认值以前是 UIModalPresentationFullScreen. iOS 13改了. 我们需要在调整前更改目标控制器的 modalPresentationStyle为UIModalPresentationFullScreen.

你也可以添加分类, 重写presentViewController方法. 以下是我的尝试, 有任何以外情况概不负责:
(分类重写原类方法, 不需要头文件引用, 即可全局起作用. 真让人不敢相信!)
分类Category重写原类方法的一些注意事项

#import <objc/runtime.h>

#import "UIViewController+PresentViewControllerOnIOS13.h"

@implementation UIViewController (PresentViewControllerOnIOS13)

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-protocol-method-implementation"

-(void)presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion{
    @try {
        
        //分类重写原类方法时, 类的方法列表, 分类的冲洗会排在前边, 原方法会排在最后. 执行时, 只有第一个会响应, 即原方法不会响应, 我们想要执行原方法, 无法使用super, 需要遍历原类的方法列表, 找到最后一个同名方法(即原方法), 然后获取方法的指针并执行
        
        viewControllerToPresent.modalPresentationStyle = UIModalPresentationFullScreen;
        
        u_int count;
        
        Method *methodsArr = class_copyMethodList([UIViewController class], &count);
        NSInteger index = 0;
        //NSLog(@"methodName begin----------------------------------------");
        for (int i = 0; i < count; i++) {
            SEL method = method_getName(methodsArr[i]);
            NSString *methodName = [NSString stringWithCString:sel_getName(method) encoding:NSUTF8StringEncoding];
            //NSLog(@"%@",methodName);
            if ([methodName isEqualToString:@"presentViewController:animated:completion:"]) {
                index = i;
                NSLog(@"methodName get ---> %d",i);
            }
        }
        //NSLog(@"methodName end----------------------------------------");
        
        SEL sel = method_getName(methodsArr[index]);
        IMP imp = method_getImplementation(methodsArr[index]);
        
        ((void (*)(id, SEL, id, BOOL, id))imp)(self, sel, viewControllerToPresent, flag, completion);
        
    } @catch (NSException *exception) {
        NSLog(@"presentViewController error %@",exception);
    }
}

#pragma clang diagnostic pop

@end

2.关于访问修改私有变量造成的闪退

//这样会闪退
//self.numTextField.placeholder = @"请输入手机号";
//[self.numTextField setValue:[UIColor getColorWithHexString:loginGray] forKeyPath:@"placeholderLabel.textColor"];
//[self.numTextField setValue:[UIFont fontWithName:@"Helvetica" size:Width414(14)] forKeyPath:@"placeholderLabel.font"];

//改成这样
self.numTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入手机号" attributes:@{NSFontAttributeName:[UIFont fontWithName:@"Helvetica" size:Width414(14)],NSForegroundColorAttributeName:[UIColor getColorWithHexString:loginGray]}];

3.如何让夜间模式(深色模式)不影响你的程序(如何关闭深色模式)

全局关闭深色模式

info.plist里添加以下配置

<key>UIUserInterfaceStyle</key>
<string>Light</string>

单个控制器关闭深色模式

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 13) {
        self.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;
    }
}

对背景色产生影响(如果不关闭深色模式)

ios13中如果你对控制器的view的backgroundColor不做设置, 背景色就会受到夜间模式的影响.
如果你不想你的app受到深色模式的影响(完全适配需要很大的工作量.), 你需要对为你的控制器的的view.backgroundColor设置为白色. 如果有tableview, 还需要吧cell的背景颜色也手动设置上. (可以设置clearColor 或者whiteColor)

对字体颜色产生影响(如果不关闭深色模式)

如果label的字体不设置颜色, 也会受到深色模式影响(夜间模式文字为白色, 背景为深色. 深色模式关闭时反之).

对tabbar的颜色(如果不关闭深色模式)

UIStatusBarStyleDefault 不再是黑色, 而是随深色模式可变(深色模式下为白色, 反之黑色)
iOS 13新增UIStatusBarStyleDarkContent 是指黑色

- (UIStatusBarStyle)preferredStatusBarStyle{
    BOOL isIOS13 = [[[UIDevice currentDevice] systemVersion] floatValue] >= 13 ? YES : NO;
    UIStatusBarStyle style = isIOS13 ? UIStatusBarStyleDarkContent : UIStatusBarStyleDefault;
    return style;
}

4.iOS 13的tabbar设置问题(tabbar的顶部横线设置失效/tabBarItem颜色失效)

问题1:
tabbar的背景色 和tabbar的顶部横线设置在iOS 13失效了.

问题2:
tabBarItem颜色失效.
隐藏tabbar以后再显示, 设置的tabBarItem颜色失效,颜色还原了(蓝色)(hidesBottomBarWhenPushed)

已知包括两种情况:
1.push控制器时, 目标控制器 vc.hidesBottomBarWhenPushed = YES ; 以隐藏底部tabbar. 返回后, 再点击其他tabbarBtn切换页面时, 点到谁谁变蓝.
2.当presentViewController模态一个控制器出来的时候, vc.modalPresentationStyle = UIModalPresentationFullScreen; 全屏模态的时候, 也相当于隐藏了tabbar, 也会出现上述现象. (但是如果不这只modalPresentationStyle属性, 即弹出页面非全屏的情况, 就不会出现tabBarItem颜色还原的问题)

解决方法:
iOS 13 新增了UITabBarAppearance对象, 用来统一管理tabbar的样式, 包括tabBarItem的颜色/tabbar的背景颜色/tabbar顶部横线的颜色 等都可以统一设置

#pragma mark - 设置tabbar的颜色样式
-(void)setTabBarColorStyle{
    
    //tabBarItem title 选中颜色
    NSMutableDictionary<NSAttributedStringKey, id> *selectedAttributes = [NSMutableDictionary dictionary];
    selectedAttributes[NSForegroundColorAttributeName] = kTabBarColor_Selected;
    
    //tabBarItem title 未选中颜色
    NSMutableDictionary<NSAttributedStringKey, id> *normalAttributes = [NSMutableDictionary dictionary];
    normalAttributes[NSForegroundColorAttributeName] = kTabBarColor_Normal;
    
    //tabbar 背景颜色
    UIColor *bgColor = RGBCOLOR(255, 255, 255);
    UIImage *bgImage = [self createImageForColor:bgColor withImageSize:CGSizeMake(kScreenShortSide, kTabBarHeight)];
    
    //tabbar上边的一条灰线
    UIColor *shadowImageColor = RGBCOLOR(250, 250, 250);
    UIImage *shadowImage = [self createImageForColor:shadowImageColor withImageSize:CGSizeMake(kScreenShortSide, 1)];
    
    if (IOS13) {
        UITabBarAppearance *tabBarAppearance = [[UITabBarAppearance alloc] init];
        //设置 tabBarItem title 颜色
        tabBarAppearance.stackedLayoutAppearance.selected.titleTextAttributes = selectedAttributes.copy;
        tabBarAppearance.stackedLayoutAppearance.normal.titleTextAttributes = normalAttributes.copy;
        //设置 tabbar背景色
        tabBarAppearance.backgroundImage = bgImage;
        //设置 tabbar上边的一条灰线
        tabBarAppearance.shadowColor = shadowImageColor;
        self.tabBar.standardAppearance = tabBarAppearance;
    }
    else{
        //设置 tabBarItem title 颜色
        for (UIViewController *vc in self.viewControllers) {
            [vc.tabBarItem setTitleTextAttributes:normalAttributes.copy forState:UIControlStateNormal];
            [vc.tabBarItem setTitleTextAttributes:selectedAttributes.copy forState:UIControlStateSelected];
        }
        //设置 tabbar背景色
        [self.tabBar setBackgroundImage:bgImage];
        //设置 tabbar上边的一条灰线
        [self.tabBar setShadowImage:shadowImage];
    }
}

#pragma mark - 指定颜色和size生成图片
-(UIImage *)createImageForColor:(UIColor *)color withImageSize:(CGSize)size{
    
    CGRect rectBG = CGRectMake(0, 0, size.width, size.height);
    UIGraphicsBeginImageContext(rectBG.size);
    CGContextRef contextBG = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(contextBG, color.CGColor);
    CGContextFillRect(contextBG, rectBG);
    UIImage *imgBG = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return imgBG;
}

5.Xcode11 缺失库文件导入libstdc-6.0.9

libstdc-6.0.9 文件下载

一共四个文件夹, 按照README里写的添加, 需要注意的是第一个文件夹的位置由:
/Applications/Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/usr/lib/
【变更为】
/Applications/Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/usr/lib/

链接:https://www.jianshu.com/p/d2a0fa7bcbef

6.Xcode11新增SceneDelegate后如何更改window的根控制器, 并兼容iOS13以下的版本

Xcode新增SceneDelegate来管理window等生命周期, 如果需要更改window的默认根控制器, 需要在 SceneDelegate 中的
- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions
代理方法中设置, 设置如下:

- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
    // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
    // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
    // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
    
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[ViewController new]];
    nav.title = @"直播";
    UITabBarController *tabVC = [[UITabBarController alloc] init];
    tabVC.viewControllers = @[nav];
    
    self.window.rootViewController = tabVC;
    [self.window makeKeyAndVisible];
}

但是该方法只会在iOS13版本才执行, 如果是iOS13以下版本, 依然需要在 AppDelegate中的
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
方法中修改window的根控制器.

// 1. AppDelegate中添加window属性
@property (strong, nonatomic) UIWindow *window;

// 2.设置window的根控制器
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    if ([[[UIDevice currentDevice] systemVersion] floatValue] < 13) {
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[ViewController new]];
        nav.title = @"直播";
        UITabBarController *tabVC = [[UITabBarController alloc] init];
        tabVC.viewControllers = @[nav];

        self.window.rootViewController = tabVC;
        [self.window makeKeyAndVisible];
    }
    
    return YES;
}

注意, 即使是iOS13以下版本, 也会执行 AppDelegate中的
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 代理, 所以一些第三方的权限验证还是可以部分版本全部写在这个代理方法中的.

6.1 使用SceneDelegate管理生命周期的风险: 使用UIAlertView闪退

https://blog.csdn.net/qq_18683985/article/details/104041447

6.2 iOS13不使用SceneDelegate

  1. 去掉info.plist 的Application Scene Manifest选项
  2. 对应的SceneDelegate删除掉。
  3. AppDelegate注释掉如下代理方法:
- (UISceneConfiguration *)application:(UIApplication *)application configurationForConnectingSceneSession:(UISceneSession *)connectingSceneSession options:(UISceneConnectionOptions *)options

- (void)application:(UIApplication *)application didDiscardSceneSessions:(NSSet<UISceneSession *> *)sceneSessions;
  1. AppDelegate添加如下代理方法:
-(void)applicationWillResignActive:(UIApplication *)application{
    NSLog(@"1");
}
-(void)applicationDidBecomeActive:(UIApplication *)application{
    NSLog(@"2");
}

- (void)applicationDidEnterBackground:(UIApplication *)application{
    NSLog(@"3");
}

-(void)applicationWillEnterForeground:(UIApplication *)application{
    NSLog(@"4");
}

-(void)applicationWillTerminate:(UIApplication *)application{
    NSLog(@"5");
}

-(void)applicationDidFinishLaunching:(UIApplication *)application{
    NSLog(@"6");
}

待续...

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 230,321评论 6 543
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 99,559评论 3 429
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 178,442评论 0 383
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 63,835评论 1 317
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 72,581评论 6 412
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 55,922评论 1 328
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 43,931评论 3 447
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 43,096评论 0 290
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 49,639评论 1 336
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 41,374评论 3 358
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 43,591评论 1 374
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 39,104评论 5 364
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 44,789评论 3 349
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 35,196评论 0 28
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 36,524评论 1 295
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 52,322评论 3 400
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 48,554评论 2 379

推荐阅读更多精彩内容