如何在SwiftUI中缩小一个填充以为另一个填充提供空间?

时间:2020-10-09 21:08:39

标签: ios swift swiftui

我有一个标题,三个图像和一个文本按钮。在较小的屏幕上,由于我已经通过.layoutPriority(..)提供了布局优先级层次结构,因此我希望标题的填充物会缩小以提供文本按钮的填充物。下面的代码不起作用时有什么想法吗?

enter image description here

import SwiftUI
import SwiftSVG

struct GenderView: View {

    public let didProvideGender: ((_ gender: Gender) -> Void)
    public let didCancel: (() -> Void)

    var body: some View {
        VStack (alignment:.center) {
            Text("Your Gender")
                .font(Font.system(size: 40.0, weight: .bold))
                .fontWeight(.bold)
                .padding(40.0) // I would expect this to shrink as space becomes less! 
                .multilineTextAlignment(.center)
                .layoutPriority(0)
                .fixedSize(horizontal: false, vertical: false)
            ForEach([Gender.Female, Gender.Male, Gender.NonBinary], id: \.self) { gender in
                Button(action: {
                    self.didProvideGender(gender)
                }) {
                    GenderSVG(gender: gender)
                }.scaledToFit()
                 .frame(width: 150, height: 150)
            }
            Button(action: {
                self.didCancel()
            }) {
                Text("I do not want to provide my gender at this time.")
                    .font(Font.system(size: 13, weight: .regular))
                    .foregroundColor(Color.black)
                    .underline()
                    .padding(.all, 10)
                    .layoutPriority(1) // Setting higher priority than that of the title!
            }
        }
    }
}

struct GenderView_Previews: PreviewProvider {
    static var previews: some View {
        GenderView(didProvideGender: { (gender) in
            
        }) {
            
        }
    }
}

// Below code is just for showing three images; you could replace it with Image(..).
enum Gender: String {
    case Male
    case Female
    case NonBinary
}

struct GenderSVG: UIViewRepresentable {

    let size: CGSize = CGSize(width: 150, height: 150)
    let gender: Gender

    static func bundleSVGName(gender: Gender) -> String {
        switch gender {
        case .Male:
            return "MaleSVG"
        case .Female:
            return "FemaleSVG"
        case .NonBinary:
            return "NonBinarySVG"
        }
    }

    func makeUIView(context: Context) -> SVGView {
        let view = SVGView(SVGNamed: GenderSVG.bundleSVGName(gender: gender))
        view.bounds = CGRect(origin: .zero, size: size)
        view.backgroundColor = .white
        return view
    }

    func updateUIView(_ view: SVGView, context: Context) {
    }

}


0 个答案:

没有答案