iOS 13 Xcode11 适配

1.

a.解决友盟 LSDefaults 崩溃

添加扩展类冷处理
+ (void)load{
    
    SEL originalSelector = @selector(doesNotRecognizeSelector:);
    SEL swizzledSelector = @selector(sw_doesNotRecognizeSelector:);
    
    Method originalMethod = class_getClassMethod(self, originalSelector);
    Method swizzledMethod = class_getClassMethod(self, swizzledSelector);
    
    if(class_addMethod(self, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod))){
        class_replaceMethod(self, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
    }else{
        method_exchangeImplementations(originalMethod, swizzledMethod);
    }
}

+ (void)sw_doesNotRecognizeSelector:(SEL)aSelector{
    //处理 _LSDefaults 崩溃问题
    if([[self description] isEqualToString:@"_LSDefaults"] && (aSelector == @selector(sharedInstance))){
        //冷处理...
        return;
    }
    [self sw_doesNotRecognizeSelector:aSelector];
}

b.其他 问题可以直接更新友盟即可

2.UIViewController 模态VC 卡片恢复成全屏 (同上扩展类)

+(void)load
{
    Method present = class_getInstanceMethod(self, @selector(presentViewController:animated:completion:));
    Method swizzNewPresent = class_getInstanceMethod(self, @selector(DJPresentViewController:Animated:completion:));
    method_exchangeImplementations(present, swizzNewPresent);
}
-(void)DJPresentViewController:(UIViewController *)viewcontroller Animated:(BOOL)animated completion:(void (^)(void))completion{
    if (@available(iOS 13.0, *)) {
        viewcontroller.modalPresentationStyle = UIModalPresentationFullScreen;
        [self DJPresentViewController:viewcontroller Animated:animated completion:completion];
    }else{
        [self DJPresentViewController:viewcontroller Animated:animated completion:completion];
    }
}

3.QMUIkit 内部报错

a.建议pod 导入 升级repo
b.重新pod install 新版本4.0.0版本及以上都可以
c.本地有QMUIConfigurationTemplate文件报错 直接github上下载下来直接替换

4.微信 WXApi 方法报错 直接更新SDK 并更换函数方法

5.

a、UITextField 通过KVC方式修改空白提示语颜色 崩溃

[UITextField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor”];

解决

attributedPlaceholder
_textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"姓名" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14],NSForegroundColorAttributeName:[UIColor redColor]}];

b、leftView、rightView 设置出错
imageView 在 textfield.leftView 中宽高被 sizeToFit 了,imageView 中设置的宽高无效了。
(例如:rightView的子视图约束布局, 会导致rightView覆盖整个UITextField)

UIImageView *imageIcon = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 34, 30)];
imageIcon.image = [UIImage imageNamed:@"search_icon"];
imageIcon.contentMode = UIViewContentModeCenter;
 // Confuse in  iOS13  先添加一层view 再添加上去
UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 80, 50)];
leftView.backgroundColor = UIColor.clearColor;
[leftView addSubview:imageIcon];
UITextField *txtSearch = [[UITextField alloc] init];
txtSearch.leftView = leftView;
txtSearch.leftViewMode = UITextFieldViewModeAlways;

c、UISearchBar searchTextField 可以直接使用不用再次获取

5、YYTextView && YYLabel 适配 完美解决的前提是 UIColor 必须正确适配

(copy 原文地址)

YYLabel 适配 YYLabel.m 添加如下代码
#pragma mark - DarkMode Adapater

#ifdef __IPHONE_13_0
- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection{
    [super traitCollectionDidChange:previousTraitCollection];
    
    if (@available(iOS 13.0, *)) {
        if([UITraitCollection.currentTraitCollection hasDifferentColorAppearanceComparedToTraitCollection:previousTraitCollection]){
            [self.layer setNeedsDisplay];
        }
    } else {
        // Fallback on earlier versions
    }
}
#endif
YYTextView 适配 YYTextView.m 添加如下代码
#pragma mark - Dark mode Adapter

#ifdef __IPHONE_13_0
- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection{
    [super traitCollectionDidChange:previousTraitCollection];
    
    if (@available(iOS 13.0, *)) {
        if([UITraitCollection.currentTraitCollection hasDifferentColorAppearanceComparedToTraitCollection:previousTraitCollection]){
            [self _commitUpdate];
        }
    } else {
        // Fallback on earlier versions
    }
}
#endif
额外要做的事情 NSAttributedString+YYText.m 去除部分CGColor 调用
- (void)setColor:(UIColor *)color {
    [self setColor:color range:NSMakeRange(0, self.length)];
}

- (void)setStrokeColor:(UIColor *)strokeColor {
    [self setStrokeColor:strokeColor range:NSMakeRange(0, self.length)];
}

- (void)setStrikethroughColor:(UIColor *)strikethroughColor {
    [self setStrikethroughColor:strikethroughColor range:NSMakeRange(0, self.length)];
}

- (void)setUnderlineColor:(UIColor *)underlineColor {
    [self setUnderlineColor:underlineColor range:NSMakeRange(0, self.length)];
}

6、iOS13 TabBarItem setTitleTextAttributes 设置无效

    [tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                        RGB(146, 146, 146), NSForegroundColorAttributeName,
                                        [UIFont boldSystemFontOfSize:10.0f], NSFontAttributeName,
                                        nil] forState:UIControlStateNormal];

此方法设置颜色无效 更改为:

    [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:RGB(252,50,79)} forState:UIControlStateSelected];

或者是

        UITabBarAppearance *tabBarAppearance = [[UITabBarAppearance alloc] init];
        tabBarAppearance.stackedLayoutAppearance.selected.titleTextAttributes = @{NSForegroundColorAttributeName:RGB(252,50,79)};
        [[UITabBar appearance] setStandardAppearance:tabBarAppearance];

7、AppDelegate 不能再使用window 添加控制器

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法中创建根控制器,会崩溃报错:如下图
    iOS 13 Xcode11 适配_第1张图片
  • Appdelegate找不到设置window的方法。

Xcode自动新增了一个SceneDelegate文件,查找了一下官方文档WWDC2019:Optimizing App Launch
iOS13之前,Appdelegate的职责全权处理App生命周期和UI生命周期;
iOS13之后,Appdelegate的职责是:
1、处理 App 生命周期
2、新的 Scene Session 生命周期
那UI的生命周期呢?交给新增的Scene Delegate处理
用图表示就是:
iOS13之前:
iOS 13 Xcode11 适配_第2张图片
这种模式完全没问题,因为只有一个进程,只有一个与这个进程对应的用户界面
但是iOS13之后,Appdelegate不在负责UI生命周期,所有UI生命周期交给SceneDelegate处理:
iOS 13 Xcode11 适配_第3张图片
iOS 13 Xcode11 适配_第4张图片

因此初始化window方法需要改变:
现在不再Appdelegate的 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 进行初始化,转交给SceneDelegate的 willConnectToSession: 方法进行根控制器设置:

- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {

    UIWindowScene *windowScene = (UIWindowScene *)scene;
    self.window = [[UIWindow alloc] initWithWindowScene:windowScene];
    self.window.frame = windowScene.coordinateSpace.bounds;
    self.window.rootViewController = [UITabBarController new];
    [self.window makeKeyAndVisible];
}

你可能感兴趣的:(ios开发随记)