25
14

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 5 years have passed since last update.

【SwiftUI】画面いっぱいにViewを広げる

Last updated at Posted at 2019-09-29

問題を感じたView

このViewを横いっぱい広げたいなんとかしたい。

スクリーンショット 2019-09-29 21.29.13.png
struct DailyCaloriesSummaryView: View {
    
    var body: some View {
        VStack(alignment: .leading, spacing: 5) {
            Text("9月11日")
                .font(.headline)
            
            VStack(alignment: .leading, spacing: 5) {
                Text("摂取カロリー: 1800kcal")
                Text("消費カロリー: 600kcal")
                Text("差分: 1200kcal")
            }
            .font(.body)
            .padding(.leading, 15)
        }
        .background(Color(.secondarySystemFill))
    }
}

struct DailyCaloriesSummaryView_Previews: PreviewProvider {
    static var previews: some View {
        DailyCaloriesSummaryView()
    }
}

画面いっぱい広げる

.background の手前に以下のコードを差し込む

.frame(maxWidth: .infinity)
スクリーンショット 2019-09-29 21.30.42.png

画面いっぱいまで広げたうえで、左寄せする

.frame(maxWidth: .infinity, alignment: .leading)
スクリーンショット 2019-09-29 21.32.32.png

見た目を整えて最終形

カードUIの出来上がり!
スクリーンショット 2019-09-29 21.34.49.png

struct DailyCaloriesSummaryView: View {
    
    var body: some View {
        VStack(alignment: .leading, spacing: 5) {
            Text("9月11日")
                .font(.headline)
            
            VStack(alignment: .leading, spacing: 5) {
                Text("摂取カロリー: 1800kcal")
                Text("消費カロリー: 600kcal")
                Text("差分: 1200kcal")
            }
            .font(.body)
            .padding(.leading, 15)
        }
        .frame(maxWidth: .infinity, alignment: .leading)
        .padding(15)
        .background(Color(.secondarySystemFill))
        .cornerRadius(15)
    }
    
}

struct DailyCaloriesSummaryView_Previews: PreviewProvider {
    static var previews: some View {
        DailyCaloriesSummaryView()
            .padding(15)
        
    }
}

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?