简体   繁体   English

如何删除视图的默认边距,SwiftUI?

[英]How to remove default margins of View, SwiftUI?

I'm new to SwiftUI.我是 SwiftUI 的新手。 According to my picture below, there are leading and trailing margins appeared in my DialogView(white area at bottom).根据我下面的图片,我的 DialogView 中出现了前导和后边距(底部的白色区域)。 I need to remove it.我需要删除它。

在此处输入图像描述

Here's my code.这是我的代码。

struct ForgetPasswordView: View {
var body: some View {
    ZStack {
        VStack {
            HStack {
                Image("img_test").resizable()
                    .frame(width: 100, height: 100)
            }
            .frame(height: 130)
            .frame(maxWidth: .infinity)
            
            Spacer()
            DialogView()
                .background(Color.white)
                .frame(maxWidth: .infinity)
        }
        .padding(.top, 120)
        .background(Color.pink)
    }
    .edgesIgnoringSafeArea(.all)
    }
}

struct DialogView: View {
var body: some View {
    VStack(alignment: .leading, spacing: 30) {
        Text("Forget Password")
            .font(.system(size: 15, weight: .light))
            .padding(.top, 20)
        Text("Proceed to reset your password? ")
            .font(.system(size: 15, weight: .light))
        Text("If yes, please enter your existing mobile number to receive OTP")
            .font(.system(size: 15, weight: .light) )
    }
    }
}

I assume that want to keep the background of the DialogView to ignore the safe area but keep its content within the safe area.我假设要保留DialogView的背景以忽略安全区域,但将其内容保留在安全区域内。

In this case use a ZStack with a fixedSize modifier like this:在这种情况下,使用带有fixedSize修饰符的 ZStack,如下所示:

struct DialogView: View {
    var body: some View {
        ZStack {
            Color.white
                .ignoresSafeArea()
            VStack(alignment: .leading, spacing: 30) {
                Text("Forget Password")
                    .font(.system(size: 15, weight: .light))
                    .padding(.top, 20)
                Text("Proceed to reset your password? ")
                    .font(.system(size: 15, weight: .light))
                Text("If yes, please enter your existing mobile number to receive OTP")
                    .font(.system(size: 15, weight: .light) )
            }
        }
        .fixedSize(horizontal: false, vertical: true)
    }
}

Also, you'll need to remove existing .background and .frame that you add in the parent view and embed it just like DialogView() .此外,您需要删除在父视图中添加的现有.background.frame并将其嵌入,就像DialogView()一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM