LoginSignup
5
0

Xcode15&iOS17でウィジェットのプレビューが表示されない

Posted at

Xcode15betaでウィジェットを確認した所、以下の画像の警告が出てプレビューが表示されなくなっていた。(Small・Medium・Largeウィジェット共通)
スクリーンショット 2023-08-04 12.04.28.png

どうやら、iOS17の場合には、modifierでcontainerBackgroundを設定する必要があるらしい。

解消方法

各ウィジェットに共通で入れたいのでextensionで以下の処理を準備する。

extension View {
    func widgetBackground(_ color: Color) -> some View {
        if #available(iOSApplicationExtension 17.0, *) {
            return containerBackground(color, for: .widget)
        } else {
            return background(color)
        }
    }
}

Small・Medium・Largeの各Viewに共通で追加する。

    var body: some View {
        ZStack {
            // widgetの処理
        }
        .widgetBackground(.clear) //色は自由
    }
5
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
5
0