LoginSignup
2
4

More than 3 years have passed since last update.

(Z,H,V)StackとSpacerでごにょごにょした件

Last updated at Posted at 2019-08-30

この記事について

SwiftUIで
UIを作成する際にあるUIパーツのleadingと揃えて15間隔を開けるときどうやるんだろう?と考えた際
色々試してみたまだ理解がたりてないところがありますが一旦記事にしてみた感じです。

Storyboardで表現する場合

imageのLeading, Bottomに揃えて15間隔を開ける感じで表現できます。
スクリーンショット 2019-08-30 18.50.56.png

SwiftUIで表現する場合

Align Leading to: icon Equals: 15
Align Bottom to: icon Equals: 15
をSwiftUIで表現してみた際の重要箇所については以下になります。

Align Leading to: icon Equals: 15
ZStack(alignment: .leading)
padding([.leading], 15)
Align Bottom to: icon Equals: 15
VStack { }
Spacer()
.padding([.bottom], 15)

import SwiftUI

struct SampleView: View {
    var body: some View {
        ZStack(alignment: .leading){
            Image("icon")
                .resizable()
            VStack {
                Spacer()
                Text("giiiita,25")
                    .foregroundColor(Color.white)
                    .padding([.leading, .bottom], 15)
            }
        }.frame(width: 352, height: 368)
    }
}

struct SampleView_Previews: PreviewProvider {
    static var previews: some View {
        SampleView()
    }
}

スクリーンショット 2019-08-30 18.54.57(2).png

ZStackのalignmentを指定しないで表現する方法

こちらの場合
Align Leading to: icon Equals: 15をZStack(alignment: .leading)に変わり表現しているのは以下になります

HStack { }
Spacer()
.padding([.leading], 15)

import SwiftUI

struct SampleView: View {
    var body: some View {
        ZStack {
            Image("icon")
                .resizable()
            HStack {
                VStack {
                    Spacer()
                    Text("giiiita,25")
                        .foregroundColor(Color.white)
                        .padding([.leading, .bottom], 15)
                }
                Spacer()
            }
        }.frame(width: 352, height: 368)
    }
}

struct SampleView_Previews: PreviewProvider {
    static var previews: some View {
        SampleView()
    }
}

テキストの長さが増えた場合どうなる?

before
Text("giiiita,25")
after
Text("giiiita,25xxxxxxxxxxxxxx")

改行が入っている。。。
スクリーンショット 2019-08-30 19.31.29(2).png

HStack, VStackに色を付けてみた
HStack: 青
VStack: 赤

スクリーンショット 2019-08-30 19.52.10(2).png

HStack内のSpacerが効いてる分VStackの表示領域が赤のframe内になってそう

改行が入らないようにするには?

VStack, HStackの順にする

struct SampleView: View {
    var body: some View {
        ZStack {
            Image("icon")
                .resizable()
            VStack {
                Spacer()
                HStack {
                    Text("giiiita,25xxxxxxxxxxxxxx")
                        .foregroundColor(Color.white)
                        .padding([.leading, .bottom], 15)
                    Spacer()
                }.background(Color.blue)
            }.background(Color.red)            
        }.frame(width: 352, height: 368)
    }
}

struct SampleView_Previews: PreviewProvider {
    static var previews: some View {
        SampleView()
    }
}

スクリーンショット 2019-08-30 19.56.54(2).png

HStack: 青
VStack: 赤

スクリーンショット 2019-08-30 19.57.58(2).png

文字の長さに対応し縦に揃えたい場合は?

文字の長さに対応した際TextはHStackの中に含まれておりました。

HStack {
   Text("giiiita,25xxxxxxxxxxxxxx")
}

この場合単純にTextを増やしてもHStackのため縦に揃えることはできません

HStack {
   Text("giiiita,25xxxxxxxxxxxxxx")
   Text("giiiita,25xxxxxxxxxxxxxx")
}

この場合は一番最初のZ,VStackの組み合わせを使用してみると良さそう

import SwiftUI

struct SampleView: View {
    var body: some View {
        ZStack(alignment: .leading){
            Image("icon")
                .resizable()
            VStack (alignment: .leading){
                Spacer()
                Group {
                    Text("giiiita,25")
                    Text("giiiita,25xxxxxxxxxxxxxx")
                }
                    .foregroundColor(Color.white)
                    .padding([.leading, .bottom], 15)
            }
        }.frame(width: 352, height: 368)
    }
}

struct SampleView_Previews: PreviewProvider {
    static var previews: some View {
        SampleView()
    }
}

スクリーンショット 2019-08-30 20.54.44(2).png

2
4
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
2
4