美文网首页
iOS 13 适配要点总结

iOS 13 适配要点总结

作者: 白屏 | 来源:发表于2020-10-22 16:08 被阅读0次

    1. 暗黑模式Dark Mode

    如果不打算适配 Dark Mode,可以直接在Info.plist中添加一栏:User Interface Style : Light,即可在应用内禁用暗黑模式

    不过即使设置了颜色方案,申请权限的系统弹窗还是会依据系统的颜色进行显示,自己创建的 UIAlertController 就不会

    2. 第三方快捷登录Sign In with Apple

    苹果在 App Store 应用审核指南 中提到:

    如果你的应用使用了第三方或社交账号登录服务(如Facebook、Google、Twitter、LinkedIn、Amazon、微信等)来设置或验证用户的主账号,就必须把 Sign In With Apple 作为同等的选项添加到应用上。

    网易新闻的快捷登录界面

    3. 私有方法 KVC 可能导致崩溃

    并不是所有kvc都会崩溃,但是有很多以前可修改的属性都不行了,只能靠试

    // 崩溃 api。获取 _placeholderLabel 不会崩溃,但是获取 _placeholderLabel 里的属性就会
    [textField setValue:[UIColor blueColor] forKeyPath:@"_placeholderLabel.textColor"];
    [textField setValue:[UIFont systemFontOfSize:20] forKeyPath:@"_placeholderLabel.font"];
    
    // 替代方案 1,去掉下划线,访问 placeholderLabel
    [textField setValue:[UIColor blueColor] forKeyPath:@"placeholderLabel.textColor"];
    [textField setValue:[UIFont systemFontOfSize:20] forKeyPath:@"placeholderLabel.font"];
    
    // 替代方案 2
    textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"输入" attributes:@{
        NSForegroundColorAttributeName: [UIColor blueColor],
        NSFontAttributeName: [UIFont systemFontOfSize:20]
    }];
    

    4. 通知deviceToken格式变化

    iOS13之前获取token方式
    - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
        NSString *token = [deviceToken description];
        for (NSString *symbol in @[@" ", @"<", @">", @"-"]) {
            token = [token stringByReplacingOccurrencesOfString:symbol withString:@""];
        }
        NSLog(@"deviceToken:%@", token);
    }
    
    iOS13起获取token方式

    {length = 32, bytes = 0xd7f9fe34 69be14d1 fa51be22 329ac80d ... 5ad13017 b8ad0736 }

    - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
        if (![deviceToken isKindOfClass:[NSData class]]) return;
        const unsigned *tokenBytes = [deviceToken bytes];
        NSString *hexToken = [NSString stringWithFormat:@"xxxxxxxx",
                              ntohl(tokenBytes[0]), ntohl(tokenBytes[1]), ntohl(tokenBytes[2]),
                              ntohl(tokenBytes[3]), ntohl(tokenBytes[4]), ntohl(tokenBytes[5]),
                              ntohl(tokenBytes[6]), ntohl(tokenBytes[7])];
        NSLog(@"deviceToken:%@", hexToken);
    }
    

    5. 模态弹窗样式

    苹果将 UIViewController 的 modalPresentationStyle 属性的默认值改成了新加的一个枚举值 UIModalPresentationAutomatic,对于多数 UIViewController,此值会映射成 UIModalPresentationPageSheet,而以前我们都是用全屏fullScreen的样式

    特别注意:非全屏情况下,将这个页面弹出的那个 ViewController 不会依次调用 viewWillDisappear 和 viewDidDisappear。然后在这个页面被 dismiss 的时候,将他弹出的那个 ViewController不会依次调用 viewWillAppear 和 viewDidAppear

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
            let dest = HomeVC()
            // 修改弹出样式
            dest.modalPresentationStyle = .fullScreen
            present(dest, animated: true, completion: nil)
        }
    

    6. 导航栏左右按钮边距

    从 iOS 11 开始,UINavigationBar 使用了自动布局,左右两边的按钮到屏幕之间会有 16 或 20 的边距。
    为了避免点击到间距的空白处没有响应,通常做法是:定义一个 UINavigationBar 子类,重写 layoutSubviews 方法,在此方法里遍历 subviews 获取 _UINavigationBarContentView,并将其 layoutMargins 设置为 UIEdgeInsetsZero

    iOS 13:此方式会crash,使用设置 frame 的方式,让 _UINavigationBarContentView 向两边伸展,从而抵消两边的边距

    import UIKit
    
    class SMNavigationBar: UINavigationBar {
    
        override func layoutSubviews() {
            
            super.layoutSubviews()
    
            for subview in subviews {
                if NSStringFromClass(subview.classForCoder).contains("_UINavigationBarContentView") {
                    if (UIDevice.current.systemVersion as NSString).doubleValue >= 13.0 {
                        let margins = subview.layoutMargins
                        subview.frame = CGRect(x: -margins.left + 10, y: -margins.top, width: margins.left + margins.right + subview.frame.size.width, height: margins.top + margins.bottom + subview.frame.size.height)
                    }else {
                        subview.layoutMargins = UIEdgeInsets.zero
                    }
                }
            }
        }
    }
    

    7. LaunchImage 被弃用

    是时候跟LaunchImage告别了, iOS 8 苹果引入了 LaunchScreen,从2020年4月开始,所有支持 iOS 13 的 App 必须提供 LaunchScreen.storyboard,否则将无法提交到 App Store 进行审批

    8. UISegmentedControl 默认样式改变

    默认样式变为白底黑字,变得有点拟物化的感觉,原本设置选中颜色的 tintColor 已经失效,新增了 selectedSegmentTintColor 属性用以修改选中的颜色

    注意:通过selectedSegmentTintColor很难精准跳转颜色,比如设置背景色为白色,是无效的(它会自动调整一下颜色对比),这个时候要通过背景图片来设置

    假设做一个选中红底白字,未选中白底红字的分段控制器

    image.png
        /// 定制分段控制器样式
        private func customStyle() {
            if #available(iOS 13.0, *) {
                control.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor.red], for: UIControl.State.normal)
                control.setBackgroundImage(imageFromColor(color: UIColor.white), for: UIControl.State.normal, barMetrics: .default)
                control.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor.white], for: UIControl.State.selected)
                control.setBackgroundImage(imageFromColor(color: UIColor.red), for: UIControl.State.selected, barMetrics: .default)
                control.layer.borderColor = UIColor.red.cgColor
                control.layer.borderWidth = 1.0
            } else {
                control.backgroundColor = .white
                control.tintColor = .red
            }
        }
        
        /// 把颜色转成图片
        private func imageFromColor(color: UIColor) -> UIImage {
            let rect = CGRect(x: 0, y: 0, width: 1, height: 1)
            UIGraphicsBeginImageContext(rect.size)
            let context = UIGraphicsGetCurrentContext()
            context?.setFillColor(color.cgColor)
            context?.fill(rect)
            let theImage = UIGraphicsGetImageFromCurrentImageContext()!
            UIGraphicsEndImageContext()
            return theImage
        }
    

    9. UIWindow视图管理UIScene

    使用 Xcode 11 创建的工程,运行设备选择 iOS 13.0 以下的设备,运行应用时会出现黑屏。这是因为 Xcode 11 默认是会创建通过 UIScene 管理多个 UIWindow 的应用,工程中除了 AppDelegate 外会多一个 SceneDelegate;这是为了 iPadOS 的多进程准备的,也就是说 UIWindow 不再是 UIApplication 中管理,但是旧版本根本没有 UIScene

    不搞iPad的话,如下处理:

    1. 删除SceneDelegate文件
    2. info.plist中删除Application Scene Manifest字段
    3. 在AppDelegate中添加window属性
    import UIKit
    
    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {
    
        /// 加上window属性
        var window: UIWindow?
        
        func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
            return true
        }
    }
    

    相关文章

      网友评论

          本文标题:iOS 13 适配要点总结

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