最近,我一直在努力创建一个复杂的视图,该视图使我可以在窗体下方使用选择器。在每种情况下,表单都只有两个选项,因此没有足够的数据向下滚动以获取更多数据。能够滚动此表单但不能滚动下面的Picker会使视图感觉很差。我无法将选择器放置在窗体内,否则SwiftUI会更改选择器上的样式。而且我在任何地方都找不到是否可以在不使用以下情况的情况下禁用列表/表单上的滚动:
.disable(condition)
是否有任何方法可以在不使用上述语句的情况下禁用列表或表单上的滚动? 这是我的代码供参考
VStack{
Form {
Section{
Toggle(isOn: $uNotifs.notificationsEnabled) {
Text("Notifications")
}
}
if(uNotifs.notificationsEnabled){
Section {
Toggle(isOn: $uNotifs.smartNotifications) {
Text("Enable Smart Notifications")
}
}.animation(.easeInOut)
}
} // End Form
.listStyle(GroupedListStyle())
.environment(\.horizontalSizeClass, .regular)
if(!uNotifs.smartNotifications){
GeometryReader{geometry in
HStack{
Picker("",selection: self.$hours){
ForEach(0..<24){
Text("\($0)").tag($0)
}
}
.pickerStyle(WheelPickerStyle())
.frame(width:geometry.size.width / CGFloat(5))
.clipped()
Text("hours")
Picker("",selection: self.$min){
ForEach(0..<61){
Text("\($0)").tag($0)
}
}
.pickerStyle(WheelPickerStyle())
.frame(width:geometry.size.width / CGFloat(5))
.clipped()
Text("min")
}
答案 0 :(得分:5)
在这里
使用我的帖子SwiftUI: How to scroll List programmatically [solution]?中的方法,可以添加以下扩展名
extension ListScrollingProxy {
func disableScrolling(_ flag: Bool) {
scrollView?.isScrollEnabled = !flag
}
}
并将其用作上面的演示示例
struct DemoDisablingScrolling: View {
private let scrollingProxy = ListScrollingProxy()
@State var scrollingDisabled = false
var body: some View {
VStack {
Button("Scrolling \(scrollingDisabled ? "Off" : "On")") {
self.scrollingDisabled.toggle()
self.scrollingProxy.disableScrolling(self.scrollingDisabled)
}
Divider()
List(0..<50, id: \.self) { i in
Text("Item \(i)")
.background(ListScrollingHelper(proxy: self.scrollingProxy))
}
}
}
}