英文:
SwiftUI: How can I make a Spacer in a List unselectable in editMode?
问题
I have a List view in my SwiftUI app(ios15.0 and above), and I need to add a Spacer at the end of the list to implement scrolling to the end, even if the last text has multiple lines.
然而,当列表进入编辑模式时,Spacer 也会获得一个复选标记,并且可以与最后一项一起选择。如何使 Spacer 不可选择?
I've tried adding the .disabled(true) modifier to the Spacer, but it doesn't seem to have any effect. Is there a way to make the Spacer unselectable without affecting the rest of the list?
我尝试将 .disabled(true) 修饰符添加到 Spacer,但似乎没有任何效果。有没有办法使 Spacer 无法选择,而不影响列表的其余部分?
Any help would be appreciated. Thank you!
任何帮助将不胜感激。谢谢!
英文:
I have a List view in my SwiftUI app(ios15.0 and above), and I need to add a Spacer at the end of the list to implement scrolling to the end, even if the last text has multiple lines.
However, when the list enters edit mode, the Spacer also gets a checkmark and can be selected with the last item. How can I make the Spacer unselectable?
I've tried adding the .disabled(true) modifier to the Spacer, but it doesn't seem to have any effect. Is there a way to make the Spacer unselectable without affecting the rest of the list?
Any help would be appreciated. Thank you!
code for minimum implement:
struct TestScrollView: View {
@State private var multiSelection = Set<String>()
@State private var editMode = EditMode.inactive
@State var messages: [String] = ["Item 1", "Item 2", "Item 3"]
var body: some View {
ScrollViewReader { scrollView in
Button {
editMode = EditMode.active
} label: {
Text("edit")
}
List(messages, id: \.self, selection: $multiSelection) { message in
Text(message)
.listRowSeparator(.hidden)
if(message == messages.last) {
Spacer()
.id("end_text")
.disabled(true)
}
}.onAppear() {
scrollView.scrollTo("end_text", anchor: .bottom)
}.environment(\.editMode, $editMode)
}
}
}
答案1
得分: 1
The List
初始化程序您正在使用会计算每次闭包调用的一行,每个消息都作为参数。它不会因为您将两个视图放入视图生成器闭包中而创建两行。它只能创建确切数量的 message.count
行。
因此,列表的最后一行包括 Text
和 Spacer
,这就是为什么您可以通过点击 Text
或 Spacer
中的任何一个来选择它们。
Spacer
应该是它自己的行。使用一个允许您指定列表中所有行的初始化程序。
List(selection: $multiSelection) {
ForEach(messages, id: \.self) { message in
Text(message)
.listRowSeparator(.hidden)
.id(message)
}
Spacer().id("end_text")
}
.environment(\.editMode, $editMode)
.onAppear {
scrollView.scrollTo("end_text", anchor: .bottom)
}
英文:
The List
initialiser that you are using computes one row from each invocation of the closure, with each message as the parameter. It does not create two rows just because you put two views in the view builder closure. It can only create exactly message.count
rows.
So the last row of the list consists of the Text
and the Spacer
, which is why you will select both by tapping on either the Text
or the Spacer
.
The Spacer
should be its own row. Use an initialiser that allows you to specify all the rows of the list.
List(selection: $multiSelection) {
ForEach(messages, id: \.self) { message in
Text(message)
.listRowSeparator(.hidden)
.id(message)
}
Spacer().id("end_text")
}
.environment(\.editMode, $editMode)
.onAppear {
scrollView.scrollTo("end_text", anchor: .bottom)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论