ScrollView行为异常(Xcode 11 GM种子-SwiftUI) [英] ScrollView acting weired (Xcode 11 GM seed - SwiftUI)

查看:60
本文介绍了ScrollView行为异常(Xcode 11 GM种子-SwiftUI)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作自定义列表.如果我们在scrollView中添加Encapsulated VStack并尝试从该VStack添加新行,它的行为就很奇怪.但是我们必须封装,因为在Xcode中会给出复杂的视图编译器错误".我正在提供完整的代码,以更好地理解.请尝试运行它.新元素未按预期添加,因此将所有内容向上推.

I was trying to make a custom list. And its acting weired if we add Encapsulated VStack in scrollView and try to add new row from that VStack. But we have to encapsulate because in Xcode will give "complex view complier error". I am providing full code for better understanding. Please try to run it. New element is not added as expected and its pushing everything upward.

struct RowView: View {

var body: some View {
    VStack{
          HStack{
              Spacer()

              .foregroundColor(Color.black)
              Spacer()
          }

      }
      .background(Color.white)
      .cornerRadius(13)
      .padding()
}}

struct cView:View {
@State var array: [String] = []

@State var height: CGFloat = 60
var body: some View {
    VStack{
        Button(action: {
            self.array.append("Test")
        }, label: {
            Text("Add")
        })

        VStack{

            ForEach(array, id: \.self){_ in
                 RowView()
            }
        }
        .background(Color.red)
        .cornerRadius(13)
        .padding()

    }


}}

struct ContentView : View {


@State var array: [String] = []

var body: some View {

        ScrollView{

            VStack{


                Text("d")
                    .frame(height: 90)
                VStack{
                       cView()
                }



            }
        }
        .navigationBarTitle("Test", displayMode: .automatic)


}}

推荐答案

当我重新格式化并删除未使用的内容时,我得到了:

When I reformatted and removed unused stuff I got:

struct RowView: View {
    let text: String

    var body: some View {
        VStack{
            HStack{
                Spacer()
                Text(text).foregroundColor(Color.black)
                Spacer()
            }
        }
        .background(Color.white)
        .cornerRadius(13)
        .padding()
    }
}

struct cView:View {
    @State var array: [String] = []

    @State var height: CGFloat = 60
    var body: some View {
        VStack{
            Button(
                action: { self.array.append("Test") },
                label: { Text("Add") }
            )
            ForEach(array, id: \.self){text in
                RowView(text: text)
            }
            .background(Color.red)
            .cornerRadius(13)
            .padding()
        }
    }
}

struct ContentView : View {
    var body: some View {
        List {
            VStack{
                Text("d")
                cView()
            }
        }
    }
}

ScrollView是真正的PITA,它讨厌Text,这就是为什么我用List替换了它.RowView缺少文本恕我直言,所以我放入其中.ContentView中的数组从未使用过,因此我将其删除了,同样,navigatinBarTitle需要一个NavigationView.

ScrollView is a real PITA and it hates Text which is why I replaced it with a List. RowView was missing a Text IMHO so I put one in. The array in ContentView was never used so I removed it, similarly a navigatinBarTitle needs a NavigationView.

这并不是一个真正的答案,因为它使用List而不是ScrollView,但它确实指出了问题所在.这也很奇怪,因为每件事都在单个列表"行中,但我尝试尽可能少地更改.

This isn't really an answer as it uses List instead of ScrollView but it does point to where your problems lie. It is also very strange as every thing is in a single List row but I tried to change as little as possible.

您可能想尝试在代码上运行SwiftLint.我经常发誓,特别是当它抱怨枚举开关的循环复杂性但确实改善了我的代码时.

You might like to try running SwiftLint on your code. I often swear at it, especially when it complains about the cyclomatic complexity of my enum switches but it does improve my code.

这篇关于ScrollView行为异常(Xcode 11 GM种子-SwiftUI)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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