2

下面的代码是创建一个自定义按钮

import SwiftUI

let skyBlue = Color(red: 75/255, green: 170/255, blue: 193/255)

struct RegisterButtonView: View {
    var body: some View {
        GeometryReader { geo in
            HStack {
                Spacer()

                Text("Register")
                    .foregroundColor(Color(.white))
                    .font(.system(size: geo.size.width / 20))
                    .bold()

                Spacer()
            }
            .frame(width: geo.size.width * 4/9, height: geo.size.height / 7)
            .background(skyBlue)
            .cornerRadius(60)
        }

    }
}

struct RegisterButtonView_Previews: PreviewProvider {
    static var previews: some View {
        RegisterButtonView()
    }
}

但是,整个按钮视图包含了 GeometryReader 引入的额外空间,因此当我将此视图放到根视图时,无法消除空间。

预览图像

4

1 回答 1

0

您可能需要直接设置 GeoReader 的框架。

    struct RegisterButtonView: View {
var body: some View {
    GeometryReader { geo in
        HStack {
            Spacer()

            Text("Register")
                .foregroundColor(Color(.white))
                .font(.system(size: geo.size.width / 20 * 9 / 4))
                .bold()

            Spacer()
        }
        .frame(width: geo.size.width , height: geo.size.height )
        .background(skyBlue)
        .cornerRadius(60)
    }.frame(width: UIScreen.main.bounds.width * 4/9 , height: UIScreen.main.bounds.height / 7)

}
}
于 2020-02-28T21:51:38.637 回答