使用swiftui,滚动时,vstack内部缩小并扩展

发布于 2025-02-11 06:24:33 字数 7546 浏览 1 评论 0原文

我正在尝试使用按钮和按钮操作的选项制作简单的测试应用程序。

目标是iOS 13.0,用于向后兼容。

就像其他iOS应用程序一样,期望光滑的滚动滚动,但它很厉害。 某些VSTACK不断缩小并扩展,不断改变滚动的高度,因此我认为这会使它感到震惊。

这是视频: https://i.sstatic.net/7iefo.jpg

如果我停下来就在口吃点,有缩水的Vstack。

您可以在视频结束时看到缩水的Vstack。

这是主视图的代码。 (缩小的部分是部分。)

import SwiftUI

struct ContentView: View {
    @Environment(\.verticalSizeClass) var verticalSizeClass: UserInterfaceSizeClass?
    @Environment(\.horizontalSizeClass) var horizontalSizeClass: UserInterfaceSizeClass?
    
    var body: some View {
        if(verticalSizeClass == .regular && horizontalSizeClass == .compact) {
            VStack {
                List {
                    SectionA()
                    SectionB()
                    SectionC()
                    SectionD()
                    SectionE()
                    SectionF()
                    SectionG()
                    SectionH()
                }
                
                OtherView()
            }
        } else {
            HStack {
                List {
                    SectionA()
                    SectionB()
                    SectionC()
                    SectionD()
                    SectionE()
                    SectionF()
                    SectionG()
                    SectionH()
                }
                
                OtherView()
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

和Heres的其他部分的简单示例(A到G是相似的。)

import SwiftUI

struct SectionF: View {
    @State var bool1 : Bool = true;
    
    @State var text1: String = "text"
    @State var text2: String = "text"
    @State var text3: String = "text"
    @State var text4: String = "text"
    
    
    var body: some View {
        Section(header: Text("Section F")) {
            Button{
            } label: {
                Text("button")
            }
            
            VStack(alignment: .leading) {
                Button{
                } label: {
                    Text("button: \(bool1 ? "true" : "false")")
                }
                Toggle("bool", isOn: $bool1)
            }
            
            
            VStack(alignment: .leading) {
                Button{
                } label: {
                    Text("button")
                }
                HStack {
                    Text("text: ")
                    TextField("text", text: $text1)
                }
                HStack {
                    Text("text: ")
                    TextField("text", text: $text2)
                }
                HStack {
                    Text("text: ")
                    TextField("text", text: $text3)
                }
                HStack {
                    Text("text: ")
                    TextField("text", text: $text4)
                }
            }
            
            VStack(alignment: .leading) {
                Button{
                } label: {
                    Text("button")
                }
                HStack {
                    Text("text: ")
                    TextField("text", text: $text1)
                }
            }
        }
    }
}

struct SectionF_Previews: PreviewProvider {
    static var previews: some View {
        Form {
            SectionF()
        }
        
    }
}

,这是可怕的 h 。。

import SwiftUI

struct SectionH: View {
    @State var text1: String = "text"
    @State var text2: String = "text"
    @State var picker1: String = "1"
    var picker1s = ["1", "2"]
    @State var text3: String = "text"
    @State var text4: String = "text"
    
    
    @State var bool1 : Bool = true;
    @State var text5: String = "text"
    @State var bool2 : Bool = true;
    @State var bool3 : Bool = true;
    @State var number1: String = "0"
    
    
    var body: some View {
        Section(header: Text("Section H")) {
            VStack(alignment: .leading) {
                Button{
                } label: {
                    Text("button")
                }
                
                HStack {
                    Text("text: ")
                    TextField("text", text: $text1)
                }
                
                HStack {
                    Text("text: ")
                    TextField("text", text: $text2)
                }
                VStack(alignment: .leading) {
                    Text("picker: \(picker1)")
                    Picker("picker", selection: $picker1) {
                        ForEach(picker1s, id: \.self) {
                          Text($0)
                        }
                    }.pickerStyle(.segmented)
                }
                
                HStack {
                    Text("text: ")
                    TextField("text", text: $text3)
                }
                
                HStack {
                    Text("text: ")
                    TextField("text", text: $text4)
                }
            }
        
        
            Button {
            } label: {
                Text("Button")
            }
            
            VStack(alignment: .leading) {
                Button {
                } label: {
                    Text("Button")
                }
                VStack {
                    
                    HStack {
                        Text("text: ")
                        TextField("text", text: $text1)
                    }
                    HStack {
                        Text("text: ")
                        TextField("text", text: $text2)
                    }
                    VStack(alignment: .leading) {
                        Text("picker: \(picker1)")
                        Picker("picker", selection: $picker1) {
                            ForEach(picker1s, id: \.self) {
                              Text($0)
                            }
                        }.pickerStyle(.segmented)
                    }
                }
                
                //**this is shrinking VStack!**
                VStack {
                    Toggle("bool", isOn: $bool1)
                    
                    HStack {
                        Text("text: ")
                        TextField("text", text: $text3)
                    }
                    
                    HStack {
                        Text("text: ")
                        TextField("text", text: $text5)
                    }
                    
                    Toggle("bool", isOn: $bool2)
                    
                    Toggle("bool", isOn: $bool3)
                    
                    HStack {
                        Text("number: ")
                        TextField("number", text: $number1)
                    }
                }
                
            }
        }
    }
}

struct SectionH_Previews: PreviewProvider {
    static var previews: some View {
        Form {
            SectionH()
        }
    }
}

这是我尝试的:

  • 形式
  • 更改的部分订单
  • 删除V/HSTACK通过方向更改&lt ;--将口吃!
  • 从SectionH sectionh divide sectionhs选项中减少选项
  • 到2 vstack&lt ;--将选项应用于上面的代码。

我是Swift和Swiftui的新手,我不知道为什么会发生这种情况,并且无法获得解决此问题的任何线索。

谢谢。

更新

删除了extracth,而不是修复。

删除了其他浏览器,而不是修复。

我认为有一个线索有关是否具有定向。

I'm trying to make simple testing app with Buttons and Options for Button action.

target is iOS 13.0 for backward compatibility.

expected smooth scroll just like other iOS app does, but it stutters.
some VStack is continuously shrink and expand, keep changing scrolls height so it stutters I think.

here's video: https://i.sstatic.net/7IEfo.jpg

if I stop just right at stutter-bottom point, there's shrunken VStack.

you can see that shrunken VStack at end of video.

here's the code of main View.
(shrunken Section is SectionH.)

import SwiftUI

struct ContentView: View {
    @Environment(\.verticalSizeClass) var verticalSizeClass: UserInterfaceSizeClass?
    @Environment(\.horizontalSizeClass) var horizontalSizeClass: UserInterfaceSizeClass?
    
    var body: some View {
        if(verticalSizeClass == .regular && horizontalSizeClass == .compact) {
            VStack {
                List {
                    SectionA()
                    SectionB()
                    SectionC()
                    SectionD()
                    SectionE()
                    SectionF()
                    SectionG()
                    SectionH()
                }
                
                OtherView()
            }
        } else {
            HStack {
                List {
                    SectionA()
                    SectionB()
                    SectionC()
                    SectionD()
                    SectionE()
                    SectionF()
                    SectionG()
                    SectionH()
                }
                
                OtherView()
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

and heres simple examples of other Sections(A to G are similar.)

import SwiftUI

struct SectionF: View {
    @State var bool1 : Bool = true;
    
    @State var text1: String = "text"
    @State var text2: String = "text"
    @State var text3: String = "text"
    @State var text4: String = "text"
    
    
    var body: some View {
        Section(header: Text("Section F")) {
            Button{
            } label: {
                Text("button")
            }
            
            VStack(alignment: .leading) {
                Button{
                } label: {
                    Text("button: \(bool1 ? "true" : "false")")
                }
                Toggle("bool", isOn: $bool1)
            }
            
            
            VStack(alignment: .leading) {
                Button{
                } label: {
                    Text("button")
                }
                HStack {
                    Text("text: ")
                    TextField("text", text: $text1)
                }
                HStack {
                    Text("text: ")
                    TextField("text", text: $text2)
                }
                HStack {
                    Text("text: ")
                    TextField("text", text: $text3)
                }
                HStack {
                    Text("text: ")
                    TextField("text", text: $text4)
                }
            }
            
            VStack(alignment: .leading) {
                Button{
                } label: {
                    Text("button")
                }
                HStack {
                    Text("text: ")
                    TextField("text", text: $text1)
                }
            }
        }
    }
}

struct SectionF_Previews: PreviewProvider {
    static var previews: some View {
        Form {
            SectionF()
        }
        
    }
}

and here's dreaded Section H.

import SwiftUI

struct SectionH: View {
    @State var text1: String = "text"
    @State var text2: String = "text"
    @State var picker1: String = "1"
    var picker1s = ["1", "2"]
    @State var text3: String = "text"
    @State var text4: String = "text"
    
    
    @State var bool1 : Bool = true;
    @State var text5: String = "text"
    @State var bool2 : Bool = true;
    @State var bool3 : Bool = true;
    @State var number1: String = "0"
    
    
    var body: some View {
        Section(header: Text("Section H")) {
            VStack(alignment: .leading) {
                Button{
                } label: {
                    Text("button")
                }
                
                HStack {
                    Text("text: ")
                    TextField("text", text: $text1)
                }
                
                HStack {
                    Text("text: ")
                    TextField("text", text: $text2)
                }
                VStack(alignment: .leading) {
                    Text("picker: \(picker1)")
                    Picker("picker", selection: $picker1) {
                        ForEach(picker1s, id: \.self) {
                          Text($0)
                        }
                    }.pickerStyle(.segmented)
                }
                
                HStack {
                    Text("text: ")
                    TextField("text", text: $text3)
                }
                
                HStack {
                    Text("text: ")
                    TextField("text", text: $text4)
                }
            }
        
        
            Button {
            } label: {
                Text("Button")
            }
            
            VStack(alignment: .leading) {
                Button {
                } label: {
                    Text("Button")
                }
                VStack {
                    
                    HStack {
                        Text("text: ")
                        TextField("text", text: $text1)
                    }
                    HStack {
                        Text("text: ")
                        TextField("text", text: $text2)
                    }
                    VStack(alignment: .leading) {
                        Text("picker: \(picker1)")
                        Picker("picker", selection: $picker1) {
                            ForEach(picker1s, id: \.self) {
                              Text($0)
                            }
                        }.pickerStyle(.segmented)
                    }
                }
                
                //**this is shrinking VStack!**
                VStack {
                    Toggle("bool", isOn: $bool1)
                    
                    HStack {
                        Text("text: ")
                        TextField("text", text: $text3)
                    }
                    
                    HStack {
                        Text("text: ")
                        TextField("text", text: $text5)
                    }
                    
                    Toggle("bool", isOn: $bool2)
                    
                    Toggle("bool", isOn: $bool3)
                    
                    HStack {
                        Text("number: ")
                        TextField("number", text: $number1)
                    }
                }
                
            }
        }
    }
}

struct SectionH_Previews: PreviewProvider {
    static var previews: some View {
        Form {
            SectionH()
        }
    }
}

here's what I tried:

  • changing List to Form
  • changing Sections order
  • removing V/HStack changing by orientation <- this one removes stutter!
  • reducing options from SectionH
  • divide SectionHs options into 2 VStack <- is applied to code above.

I'm new to Swift and SwiftUI, I don't know why this happens and can't get any clues to solve this problem.

thanks in advice.

UPDATE

removed SectionH, not fixed.

removed OtherView, not fixed.

I think theres a clue on if state with orientation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文