如何适配iOS13

作者: lilei5 | 来源:发表于2019-10-08 16:44 被阅读0次

    此博客记录现有项目升级iOS13所遇到的问题及解决方式,可能并不全面,如果你遇到了其他问题,欢迎评论区留言。Xcode版本11.0;

    1、UITextField的leftView、rightView 显示异常

    textField的leftView显示异常

    如图是我们工程使用textField自定义的searchBar,在iOS13中 发现搜索图片居中显示,设置背景色,发现leftView占满了整个textField。原代码

    UIView *leftView = [UIView new];
    leftView.frame = CGRectMake(0, 0, 38, 38);
    UIImageView *imageView = [[UIImageView alloc] initWithImage:k_IMAGE(@"icon_sousuo")];
    [leftView addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.center.equalTo(leftView);
    }];
    

    产生原因: 因为leftView的子视图是使用约束布局的,虽然这里没有约束图片宽高,但是即使设置了也是一样的效果。
    解决方法:改为frame布局,代码如下

    imageView.frame = leftView.bounds;
    

    2、 UISearchBar 显示问题

    产生原因: 由于我们项目之前使用遍历searchBar子视图的方式获取searchBar中的textField,但是由于在iOS13中,textField的层级关系改变了,所以这种方式获取不到了,导致拿不到textField,所有无法设置其中文字大小和颜色。
    解决方式:iOS13中,UISeachBar多了一个属性,searchTextField,可直接设置。

     if (@available(ios 13.0,*)) {
             searchBar.searchTextField.font = [UIFont systemFontOfSize:12];
             searchBar.searchTextField.tintColor = [UIColor clearColor];
        }else{
            for (UIView *subView in [[searchBar.subviews lastObject] subviews]) {
                if ([[subView class] isSubclassOfClass:[UITextField class]]) {
                    UITextField *textField = (UITextField *)subView;
                    textField.font = [UIFont systemFontOfSize:12];
                    textField.tintColor = [UIColor clearColor];
                    break;
                }
            }
        }
    

    3、禁止KVC获取私有属性

    如上述获取searchBar中的textField,之前也可以通过KVC的方式获取:[searchBar valueForKey:@"_searchField"],iOS13请使用以上方式。
    以及之前使用KVC对textField的占位文字颜色的处理,

    [textField setValue:[UIColor red] forKeyPath:@"_placeholderLabel.textColor"];
    //替换为
    textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"输入"attributes:@{NSForegroundColorAttributeName: [UIColor red]}];
    

    也就是说,所有私有属性,现在都不能用KVC获取了😂

    4、UITabBar选中文字颜色变成蓝色

    tabbar选中文字颜色错误

    之前代码:

    //  未选中
     [[UITabBarItem appearance] setTitleTextAttributes:
         @{NSForegroundColorAttributeName:k_COLOR_HEX(@"#aaaaaa")}
                                                 forState:UIControlStateNormal];
     //  选中
     [[UITabBarItem appearance] setTitleTextAttributes:
         @{ NSForegroundColorAttributeName: k_COLOR_CUSTOM_RED} forState:UIControlStateSelected];
    

    解决方式:判断iOS13版本,设置未选中的tintColor,选中颜色就会正常😂,代码如下:

    if (@available(iOS 13.0, *)) {
            [[UITabBar appearance] setUnselectedItemTintColor:k_COLOR_HEX(@"#aaaaaa")];
        }
    

    5、模态跳转默认样式改变

    模态弹出样式
    产生原因:因为苹果将 UIViewControllermodalPresentationStyle 属性的默认值改成了新加的一个枚举值 UIModalPresentationAutomatic,对于多数 UIViewController,此值会映射成 UIModalPresentationPageSheet
    解决方式:
    方法一:
    给要模态跳转的UIViewController/UINavigationController对象设置属性nav.modalPresentationStyle = UIModalPresentationFullScreen;
    方法二:如果你们模态跳转的地方很多且没有一个共同的父类,可以通过Method Swizzling 来hook系统的方法modalPresentationStyle,返回UIModalPresentationFullScreen
    - (UIModalPresentationStyle)my_modalPresentationStyle {
        return UIModalPresentationFullScreen;
    } 
    

    项目中暂时遇到的就这些,后面有遇到会持续更新,如有不对的地方欢迎指正,感谢点赞和关注!

    相关文章

      网友评论

        本文标题:如何适配iOS13

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