Xcode 11 & iOS13 适配文档

iOS13出来有一段时间了,最近才开始适配,着实有些晚了,记录下自己踩到的坑; 持续更新...

[2019-08-26 ]: Xcode11 & iOS13真机'UIAlertView' Crash

*** Terminating app due to uncaught exception 'NSObjectNotAvailableException', reason: 'UIAlertView is deprecated and unavailable for UIScene based applications, please use UIAlertController!'

处理方式就显而易见了,替换成UIAlertController,看来标记为废弃的还是尽量不用为好

[2019-08-19]: iOS 13 beta 7 : 子线程Crash,App仍存活

2019-08-15日Apple发布了iOS 13 beta 7, 收到一系列新的反馈,其中有一个问题比较奇怪,在触发某些Case后,相关功能稳定不可用,Debug测试发现在iOS 13 beta 7中,某个子线程遇到Swift强制解包(nil!)不会导致整个App Crash掉,主线程卡顿一段时间后,仍然可以正常的操作,但是依赖Crash的子线程的任务会挂掉,具体原因暂时未知,还在调研。

[2019-07-30]: -w -Xanalyzer -analyzer-disable-all-checks

Xcode11-beta4(仅在4,1、2、3&5没有这个问题)中可能会遇到如下报错

:0: error: unknown argument: '-w'
:0: error: unknown argument: '-Xanalyzer'
:0: error: unknown argument: '-analyzer-disable-all-checks'
Command CompileSwiftSources failed with a nonzero exit code
这个是因为Cocoapods的inhibit_all_warnings!:inhibit_warnings => true导致的,需要到Podfiile中注释掉所有相关部分,如:

修改前:

# 全局关闭警告
inhibit_all_warnings! 
pod 'RxSwift', :inhibit_warnings => true

修改后:

# 全局关闭警告
# inhibit_all_warnings! 
pod 'RxSwift'#, :inhibit_warnings => true

参考链接: https://github.com/CocoaPods/CocoaPods/issues/9013


[2019-07-16]: StatusBar

之前可以使用如下方法获取或修改StatusBar的一些属性

if let statusBarWindow = UIApplication.shared.value(forKey: "statusBarWindow") as? UIWindow {
    statusBarWindow.alpha = 1 - statusBarWindow.alpha
}

现在会报错

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'App called -statusBar or -statusBarWindow on UIApplication: this code must be changed as there's no longer a status bar or status bar window. Use the statusBarManager object on the window scene instead.'

找了很久找到一些获取属性的方式,但是现在没法更改属性了,无论是直接改还是setValue(_: forKey:),都会崩掉。

if #available(iOS 13.0, *) {
    #if swift(>=5.1)
    if let statusBarManager = UIApplication.shared.keyWindow?.windowScene?.statusBarManager,
        let localStatusBar = statusBarManager.perform(Selector(("createLocalStatusBar")))?.takeRetainedValue()
            as? UIView,
        let statusBar = localStatusBar.perform(Selector(("statusBar")))?.takeRetainedValue() as? UIView,
        let _statusBar = statusBar.value(forKey: "_statusBar") as? UIView {
        print(localStatusBar, statusBar, _statusBar)
    }
    #endif
} else {
    // Fallback on earlier versions
    if let statusBarWindow = UIApplication.shared.value(forKey: "statusBarWindow") as? UIWindow {
        statusBarWindow.alpha = 1 - statusBarWindow.alpha
    }
}

私有属性KVC

使用私有API一时爽,一直使用一直爽 苹果一禁火葬场

iOS不再支持使用valueForKeysetValue: forKey等方式处理一些私有属性,下面列举一下已经发现的和替换方案。

UITextFiled: _placeholderLabel.textColor

// old code Swift
textFiled.setValue(UIColor.red, forKey: "_placeholderLabel.textColor")

替换方案

textFiled.attributedPlaceholder = NSMutableAttributedString(
    string: "Placeholder",
    attributes: [.foregroundColor: UIColor.red]
)

UISearchBar: value(forKey: "_searchField")

// old  code Swift
searchBar.value(forKey: "_searchField")

替换方案

#if swift(<5.1)
print(searchBar.value(forKey: "_searchField") ?? "Read Error")
#else
print(searchBar.searchTextField)
#endif

样式 Or 行为变更

UISegmentedControl 样式变更下图

Xcode 11 & iOS13 适配文档_第1张图片
UISegmentedControl 样式对比

处理方案:

    // swift 使用下面的方法自定义样式
    control.setTitleTextAttributes(
        [.foregroundColor: UIColor.white],
        for: .selected)

    control.setBackgroundImage(
        UIImage(named: "iamgeName"),
        for: .normal,
        barMetrics: .default)

    control.setDividerImage(
        UIImage(named: "iamgeName"),
        forLeftSegmentState: .normal,
        rightSegmentState: .normal,
        barMetrics: .default)

UIModalPresentationStyle

ViewControllerpresent 的默认style变了,如下图:

Xcode 11 & iOS13 适配文档_第2张图片
UIModalPresentationStyle 默认样式

你可能感兴趣的:(Xcode 11 & iOS13 适配文档)