LoginSignup
14
16

More than 3 years have passed since last update.

SwiftUIでViewのレイアウトを調整するStackについて調べて見ました

Posted at

SwiftUIの3つのView Layout

SwiftUIには3つのレイアウトを調整するStackView的なLayoutが存在する

  • HStack (X軸を基準として横並びにViewを並べるレイアウト)
  • VStack (Y軸を基準として縦並びにViewを並べるレイアウト)
  • ZStack (Z軸を基準として重ねてViewを載せるレイアウト)

発想がAndroidのUIに似ています。

HStackはLinearLayout(Horizontal)のそれに似ていますし、
VStackはLinearLayout(Vertical)に似ています。

ということはSwiftUIはコンポーネントの配置の方法はpaddingやmarginを設定して調整するものだろうと推測できます。

HStack (X軸を基準として横並びにViewを並べるレイアウト)

ContentView.swift
struct ContentView : View {
    var body: some View {
        HStack {
            ParentView()
            SubView()
        }
        .frame(width: 400, height: 200, alignment: .center)
            .background(Color.yellow)
    }
}

struct ParentView: View {
    var body: some View {
        Text("ParentView")
            .color(Color.white)
            .frame(width: 200, height: 200, alignment: .center)
            .background(Color.red)

    }
}

struct SubView: View {
    var body: some View {
        Text("SubView")
            .color(Color.orange)
            .frame(width: 200, height: 200, alignment: .center)
            .background(Color.blue)
    }
}

Viewの見え方

スクリーンショット 2019-07-22 23.15.44.png

VStack (Y軸を基準として縦並びにViewを並べるレイアウト)

ContentView.swift
struct ContentView : View {
    var body: some View {
        VStack {
            ParentView()
            SubView()
        }
        .frame(width: 400, height: 200, alignment: .center)
            .background(Color.yellow)
    }
}

struct ParentView: View {
    var body: some View {
        Text("ParentView")
            .color(Color.white)
            .frame(width: 200, height: 200, alignment: .center)
            .background(Color.red)

    }
}

struct SubView: View {
    var body: some View {
        Text("SubView")
            .color(Color.orange)
            .frame(width: 200, height: 200, alignment: .center)
            .background(Color.blue)
    }
}

Viewの見え方

スクリーンショット 2019-07-22 23.19.10.png

ZStack (Z軸を基準として重ねてViewを載せるレイアウト)

ContentView.swift
struct ContentView : View {
    var body: some View {
        ZStack {
            ParentView()
            SubView()
        }
        .frame(width: 300, height: 300, alignment: .center)
            .background(Color.yellow)
    }
}

struct ParentView: View {
    var body: some View {
        Text("ParentView")
            .color(Color.white)
            .frame(width: 200, height: 200, alignment: .center)
            .background(Color.red)
    }
}

struct SubView: View {
    var body: some View {
        Text("SubView")
            .color(Color.orange)
            .frame(width: 100, height: 100, alignment: .center)
            .background(Color.blue)
    }
}

Viewの見え方

スクリーンショット 2019-07-22 23.25.57.png

参考ドキュメント

14
16
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
14
16