SwiftUI 中元素之间的间距? [英] Spacing between elements in SwiftUI?

查看:52
本文介绍了SwiftUI 中元素之间的间距?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么这个间距会出现在 SwiftUI 中的 2 个元素之间?以及如何控制/修改它?我尝试向 ExtractedView 添加一些填充,但没有任何改变.看起来它是由 .frame(height: 56) 引起的,但这正是按钮的高度,所以它不应该导致任何间距.

I wonder why does this spacing appear between 2 elements in SwiftUI? And how to control/modify it? I tried adding some paddings to ExtractedView but it changed nothing. It seems like it's somehow caused by .frame(height: 56) but this is exactly the height of the button, so it shouldn't lead to any spacing.

代码:

import SwiftUI

struct FirstLaunchView: View {
    var body: some View {
        VStack {
            ZStack {
                Image("first-launch")
                    .resizable()
                    .scaledToFill()
                    .frame(height: 483)
                Text("AppName")
                    .font(.custom("Lobster", size: 24))
                    .fontWeight(.bold)
                    .foregroundColor(.white)
                    .frame(height: 483, alignment: .top)
                    .offset(x: 0, y: 61)
            }

            ZStack {
                VStack {
                    ExtractedView(title: "Start", textColor: Color.secondaryColor, bgColor: Color.primaryColor)
                    ExtractedView(title: "Log in", textColor: Color.primaryColor, bgColor: Color.secondaryColor)
                }
            }
            
            Spacer()
            
        }
        .ignoresSafeArea()
        
    }
}

struct ExtractedView: View {
    var title: String
    var textColor: Color
    var bgColor: Color
    
    var body: some View {
        ZStack {
            RoundedRectangle(cornerRadius: 27.5)
                .fill(bgColor)
                .frame(width: 279, height: 56)
            RoundedRectangle(cornerRadius: 27.5)
                .strokeBorder(Color.primaryColor, lineWidth: 2)
                .frame(width: 279, height: 56)
            Text(title)
                .font(.custom("NotoSans-Regular", size: 18))
                .fontWeight(.bold)
                .foregroundColor(textColor)
        }
        .frame(height: 56)
    }
}

推荐答案

VStack 有默认间距,所以在

VStack {
    ExtractedView(title: "Start", textColor: Color.secondaryColor, bgColor: Color.primaryColor)
    ExtractedView(title: "Log in", textColor: Color.primaryColor, bgColor: Color.secondaryColor)
}

您赋予 SwiftUI 决定内部视图之间应应用的默认间距的权利.

you give SwiftUI rights to decide which default spacing should be applied between internal views.

如果你想要明确的间距,你可以指定它,比如

If you want explicit spacing, you can specify it, like

VStack(spacing: 25) {    // << here !!
  // ... views here
}

这篇关于SwiftUI 中元素之间的间距?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆