首页 > 解决方案 > 更改图像形状后,SwiftUI 无法编译

问题描述

我对以前的设计不满意,所以我想稍微更改一下代码,但是当我尝试下面的代码时,它开始给我一个错误。Swift UI 错误处理不是最好的,所以我不知道如何修复它

这是代码

struct spanishfood: View {
    @State private var stack1 =
    [Image("spanish_omelette"),
    Image("paella"),
    Image("gazpacho"),
    Image("patatas_bravas")]


    @State private var names =
    [Text("Spanish Omelette"),
    Text("Paella"),
    Text("Gazpacho"),
    Text("Patatas Bravas")]


    var views =
    [AnyView(spanish_omelette()),
    AnyView(paella()),
    AnyView(gazpacho()),
    AnyView(patatas_bravas())]
    var body: some View {
        GeometryReader { geometry in
            ScrollView {
                VStack(spacing: 10) {
                    Text("Spanish Food")
                        .font(.title)
                        .font(Font.system(size: 36, design: .serif))
                        .foregroundColor(.black)
                        .fontWeight(.thin)
                        .bold()
                        .italic()
                        .kerning(5)
                        .background(Color.white.opacity(0.6))
                        .shadow(radius: 31)
                    ForEach(0..<self.stack1.count) { index in
                        NavigationLink(destination: self.views[index]) {
                            VStack(spacing: 10) {
                                self.stack1[index]
                                .resizable()
                                .renderingMode(.original)
                                .frame(width: UIScreen.screenWidth, height: 300)
                                .clipShape(RoundedRectangle())
                                self.names[index].foregroundColor(.white)
                                .bold()
                            }.animation(.interpolatingSpring(stiffness: 50, damping: 1))
                        }
                    }
                }.frame(width: geometry.size.width)
            }
        }
    }
}

以前的形状是圆形的,框架要小得多

标签: swiftswiftui

解决方案


通过代码阅读 this .clipShape(RoundedRectangle()),因为RoundedRectangle没有空参数构造函数。可能的修复如下

.clipShape(RoundedRectangle(cornerRadius: 12))

推荐阅读