English 中文(简体)
VStack Not Resperly to ForEach
原标题:VStack Not Resizing Properly to ForEach Contents

考虑建立以下机构(它不是试亮的显示,而是我认为能够复制我的问题的最快方式):

class IdentifiableString: Identifiable, Hashable {
    static func ==(lhs: IdentifiableString, rhs: IdentifiableString) -> Bool {
        lhs.id == rhs.id
    }
    
    func hash(into hasher: inout Hasher) {
        hasher.combine(self.id)
    }
    
    let id = UUID()
    var string = ""
}

struct ContentView: View {
    @State var textField1Text = ""
    @State var textField2Text = ""
    @State var textField3Text = ""
    @State var textField4Text = ""
    @State var textField5Text = ""
    @State var textField6Text = ""
    @State var textField7Text = ""
    @State var textField8Text = ""
    @State var textField9Text = ""
    @State var textField10Text = ""
    @State var textField11Text = ""
    @State var textField12Text = ""
    @State var textField13Text = ""
    @State var textField14Text = ""
    @State var textField15Text = ""
    
    @State var identifibleStrings =  [IdentifiableString(), IdentifiableString(), IdentifiableString(), IdentifiableString(), IdentifiableString(), IdentifiableString(), IdentifiableString(), IdentifiableString(), IdentifiableString(), IdentifiableString(), IdentifiableString(), IdentifiableString(), IdentifiableString(), IdentifiableString(), IdentifiableString()]
    
    var body: some View {
        List {
            Button("Reset", role: .destructive) {
                textField1Text = ""
                textField2Text = ""
                textField3Text = ""
                textField4Text = ""
                textField5Text = ""
                textField6Text = ""
                textField7Text = ""
                textField8Text = ""
                textField9Text = ""
                textField10Text = ""
                textField11Text = ""
                textField12Text = ""
                textField13Text = ""
                textField14Text = ""
                textField15Text = ""
                
                for identifibleString in identifibleStrings {
                    identifibleString.string = ""
                }
            }
            
            Section {
                TextField("TextField 1", text: $textField1Text)
                    .onChange(of: textField1Text) { _, newValue in
                        identifibleStrings[0].string = newValue
                    }
                TextField("TextField 2", text: $textField2Text)
                    .onChange(of: textField2Text) { _, newValue in
                        identifibleStrings[1].string = newValue
                    }
                TextField("TextField 3", text: $textField3Text)
                    .onChange(of: textField3Text) { _, newValue in
                        identifibleStrings[2].string = newValue
                    }
                TextField("TextField 4", text: $textField4Text)
                    .onChange(of: textField4Text) { _, newValue in
                        identifibleStrings[3].string = newValue
                    }
                TextField("TextField 5", text: $textField5Text)
                    .onChange(of: textField5Text) { _, newValue in
                        identifibleStrings[4].string = newValue
                    }
                TextField("TextField 6", text: $textField6Text)
                    .onChange(of: textField6Text) { _, newValue in
                        identifibleStrings[5].string = newValue
                    }
                TextField("TextField 7", text: $textField7Text)
                    .onChange(of: textField7Text) { _, newValue in
                        identifibleStrings[6].string = newValue
                    }
                TextField("TextField 8", text: $textField8Text)
                    .onChange(of: textField8Text) { _, newValue in
                        identifibleStrings[7].string = newValue
                    }
                TextField("TextField 9", text: $textField9Text)
                    .onChange(of: textField9Text) { _, newValue in
                        identifibleStrings[8].string = newValue
                    }
                TextField("TextField 10", text: $textField10Text)
                    .onChange(of: textField10Text) { _, newValue in
                        identifibleStrings[9].string = newValue
                    }
                TextField("TextField 11", text: $textField11Text)
                    .onChange(of: textField11Text) { _, newValue in
                        identifibleStrings[10].string = newValue
                    }
                TextField("TextField 12", text: $textField12Text)
                    .onChange(of: textField12Text) { _, newValue in
                        identifibleStrings[11].string = newValue
                    }
                TextField("TextField 13", text: $textField13Text)
                    .onChange(of: textField13Text) { _, newValue in
                        identifibleStrings[12].string = newValue
                    }
                TextField("TextField 14", text: $textField14Text)
                    .onChange(of: textField14Text) { _, newValue in
                        identifibleStrings[13].string = newValue
                    }
                TextField("TextField 15", text: $textField15Text)
                    .onChange(of: textField15Text) { _, newValue in
                        identifibleStrings[14].string = newValue
                    }
            }
            
            Section {
                VStack {
                    ForEach(identifibleStrings) { identifiableString in
                        if identifiableString.string != "" {
                            Text(identifiableString.string)
                        }
                    }
                }
            }
        }
    }
}

I ve discovered that when I provide all of my text fields with some kind of input, tap on the reset button, and then scroll down to my VStack, I see this:

Oversized VStack

我发现,只有我把我的“五世纪之四”字 screen倒,我才发现,它已经适当调整了:

“Properly

注: 如果在重新构造的纽芬兰语(,例如评论案文领域10-15,或用大屏幕(如iPad)操作有关装置的代码,在德国马克角上看到VStack的浏览量,那么这种行为就会出现。

我有时甚至没有足够增长才能适当展示其所有内容,但我没有能够一贯地重复这一行为。 我试图这样做,创建了一个“带有高米数据”的纽芬兰语,自动把我的所有文本领域都包含在数据上,但VStack却达到了预期的规模。

我试图在一次 <日/日/日/日/日/日/日/日/日/日/日/。 仅允许 < VVStack

 @State var resetUpdate = false
 Button("Set with Dummy Data") {
    resetUpdate = true
    ...
 }
 .onChange(of: resetUpdate) { _, newValue in
     if newValue == true {
         resetUpdate = false
     }
 }
if !resetUpdate {
    VStack {
        ...

但这没有效果。 可在以下网站查阅:http://www.un.org/Depts/DGACM/index_french.htm。 是否有人对这种行为发生的原因(以及如何防止这种行为)有任何怀疑?

问题回答

@State var ......working with Value category, eg struct, not reference category, eg }/code>,除非您使用观察框架。

采用<代码>的方法 可用于简化编码的可观测类分类编码和一些

It seems that the View does not rerender the VStack properly (probably a bug). To force it to do so, I used an .id() and change its value when the reset button is tapped.

Example code that works for me:

@Observable class IdentifiableString: Identifiable {
    let id = UUID()
    var string = "xxxx" // <--- for testing
    init() {  }
}

struct ContentView: View {
    @State private var items = [IdentifiableString]()
    @State private var id = false  // <--- here

    var body: some View {
        List {
            Button("Reset", role: .destructive) {
                items.indices.forEach{ items[$0].string = "" }
                id.toggle()  // <--- here
            }
            Section {
                ForEach($items) { $item in
                    TextField("TextField", text: $item.string)
                }
            }
            Section {
                VStack {
                    ForEach(items) { item in
                        if !item.string.isEmpty {
                            Text(item.string)
                        }
                    }
                }
                .id(id) // <--- here, or on Section, or list
            }
        }
        .onAppear {
            for _ in 0...24 { items.append(IdentifiableString())}
        }
    }
}




相关问题
How to start to create an application GUI using C#?

HI! I am new to C# and plan to use it for my application GUI. I am trying to make my GUI similar to SPSS:http://www.spss.com/images/08/statistics_screens/ez_rfm-big.jpg Is this easy in C#? Is there ...

Automatic height of edit box

My shoes application has three items stacked on top of each other (with a stack, of course), in order: A banner An edit box Two buttons in a flow What I want to do is have the banner stay at it s ...

Search by using the keyboard in a list/grid - algorithm

I need to implement a custom search in a grid and I would like to find some user interface guidelines that explain the standard way to implement it. I mean this kind of search that is initiated by ...

UI And TcpClient Issue in vb.net

I m having some problems with a small ircbot i m writing. Basically I connect to the server using a tcpclient in a seperate class, which also runs on its own thread. I want to display the server text ...

UI Convention: Shortcut key for application exit? [closed]

Is there a convention for the shortcut keys for application exit? Some applications uses Alt+X some others use Ctrl+ X and Ctrl+Q. Applications like FF and IE doesnot assign a shortcut at all. So is ...