13
9

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 1 year has passed since last update.

【SwiftUI】Viewを引数にとるViewの作成方法

Last updated at Posted at 2022-08-30

方法

今回の例は背景色を黄色にするViewを作成しています。

import SwiftUI

struct BackgroundView<T: View>: View {

    @ViewBuilder var content: () -> T

    var body: some View {
        ZStack(alignment: .center) {
            Color.yellow.edgesIgnoringSafeArea(.all)
            content()
        }
    }
}

使い方

struct ContentView: View {
    var body: some View {
        BackgroundView {
            Text("サンプル")
        }
    }
}

完成形

simulator_screenshot_BB9BB8D8-FC4F-47E2-B416-190C4ABB6E08.png

おわり

コードが綺麗になるので積極的に使っていきたいです

13
9
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
13
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?