0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Xcode12&iOS14でUIDatePickerがクラッシュするようになった件について

Last updated at Posted at 2021-02-01

はじめに

iOS14がリリースされて4ヶ月経ちましたね。
時間が経ってしまいましたがようやくXCode12に対応しました。

しかしXCode12対応版をリリースしたところ、
コードを変更した覚えの無い日付入力フォームからクラッシュ報告が届くようになりました。

クラッシュ箇所のコード

Sample.swift
@IBOutlet private weak var datePicker: UIDatePicker!
~~~中略~~~
self.datePicker.setValue(false, forKey: "highlightsToday")

原因

iOS14 (13.4) ではUIDatePickerに更新が入りました。
その際にiOS13までは見た目がwheelsのみでしたが、
スタイルのパラメータが追加され見た目が指定できるようになりました。

UIDatePicker.swift
@available(iOS 13.4, *)
public enum UIDatePickerStyle : Int {    
    /// Automatically pick the best style available for the current platform & mode.
    case automatic = 0

    /// Use the wheels (UIPickerView) style. Editing occurs inline.
    case wheels = 1

    /// Use a compact style for the date picker. Editing occurs in an overlay.
    case compact = 2

    /// Use a style for the date picker that allows editing in place.
    @available(iOS 14.0, *)
    case inline = 3
}

出典:UIDatePickerStyle

指定していない場合はもちろんautomaticになります。
そして何の見た目が使われるかは端末のバージョンに依存するようになります。

iOS13系ではwheelsになりますが、iOS14ではwheels以外になるようです。

今回の更新でXCodeを11から12に移行した為に上記コードが適用され、
見た目がautomaticになり、highlightsTodayが存在しないキーとなりクラッシュしたようです。

対応

以下のコードを追加することで見た目の指定が出来ます。
iOS13.4以降なので、Targetが13.4未満の場合は要求バージョンの指定が必要になります。

今後iOS14以降をターゲットにする新規プロジェクトではデザインする際、
どの見た目を利用するかを検討する必要があるといえます。

Sample.swift
if #available(iOS 13.4, *) {
    self.datePickerView.preferredDatePickerStyle = .wheels
}

補足

iOS14で追加された見た目は以下のようになります。
上記のようにiOS14ではUIDatePickerStyleを明記する必要があります。

OS automatic compact compact(タップ時) inline wheels
iOS14 IMG_0137.PNG IMG_0137.PNG IMG_0138.PNG IMG_0136.PNG IMG_0135.PNG
iOS13 IMG_0135.PNG - - - -

以上

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?