0

如何在全屏垂直 ScrollView (SwiftUI) 中禁用过度滚动?

https://www.youtube.com/watch?v=dOyWCDhlxv8 - 过度滚动示例(我知道,在视频旧 Android 设备上)

 var body: some View {
    GeometryReader { geometry in
        ScrollView([.vertical], showsIndicators: false)
        {
            VStack
                {
                    VStack (alignment: .leading) {

                        ForEach (0..<leftmenuuser.menuitems.count)
                        { i in
                            if (leftmenuuser.menuitems[i].index >= 0)
                            {
                                HStack {
                                    HStack (spacing: leftmenuuser.self.iconssize)
                                    {
                                        Image(leftmenuuser.menuitems[i].imagename).resizable().frame(width: leftmenuuser.self.iconssize, height: leftmenuuser.self.iconssize)

                                        Text(leftmenuuser.menuitems[i].title).foregroundColor(Color.white).font(.custom("PFDinTextCompPro-Regular", size: leftmenuuser.self.smallfontsize)).lineLimit(1)

                                        Spacer()
                                    }.padding(leftmenuuser.self.mypadding).frame(maxWidth: .infinity)
                                }.background(self.selectedindex == leftmenuuser.menuitems[i].index ? Color.yellow : Color.white).onTapGesture {
                                    leftmenuuser.menuitems[i].action(leftmenuuser.menuitems[i])
                                    self.selectedindex = leftmenuuser.menuitems[i].index
                                    self.showmenu = false
                                }
                            }
                        }
                    }
                    Spacer()
            }
            .background(Color.blue).frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .leading)
            }.background(Color.red).edgesIgnoringSafeArea(.all)
    }
}
4

1 回答 1

5

您可以关闭该特定视图的弹跳:

 var body: some View {
  init() {
    UIScrollView.appearance().bounces = false
  }
    GeometryReader { geometry in
        ScrollView([.vertical], showsIndicators: false)
        {
            VStack
                {
                    VStack (alignment: .leading) {

                        ForEach (0..<leftmenuuser.menuitems.count)
                        { i in
                            if (leftmenuuser.menuitems[i].index >= 0)
                            {
                                HStack {
                                    HStack (spacing: leftmenuuser.self.iconssize)
                                    {
                                        Image(leftmenuuser.menuitems[i].imagename).resizable().frame(width: leftmenuuser.self.iconssize, height: leftmenuuser.self.iconssize)

                                        Text(leftmenuuser.menuitems[i].title).foregroundColor(Color.white).font(.custom("PFDinTextCompPro-Regular", size: leftmenuuser.self.smallfontsize)).lineLimit(1)

                                        Spacer()
                                    }.padding(leftmenuuser.self.mypadding).frame(maxWidth: .infinity)
                                }.background(self.selectedindex == leftmenuuser.menuitems[i].index ? Color.yellow : Color.white).onTapGesture {
                                    leftmenuuser.menuitems[i].action(leftmenuuser.menuitems[i])
                                    self.selectedindex = leftmenuuser.menuitems[i].index
                                    self.showmenu = false
                                }
                            }
                        }
                    }
                    Spacer()
            }
            .background(Color.blue).frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .leading)
            }.background(Color.red).edgesIgnoringSafeArea(.all)
    }
}

或者通过将其添加到您的 AppDelegate 来为整个应用程序中的所有滚动视图:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    UIScrollView.appearance().bounces = false
}
于 2020-03-12T13:52:10.753 回答