【问题标题】:Disable overscroll in ScrollView (SwiftUI)在 ScrollView (SwiftUI) 中禁用过度滚动
【发布时间】:2020-06-24 14:06:41
【问题描述】:

如何在全屏垂直 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)
    }
}

【问题讨论】:

    标签: swift scrollview overscroll


    【解决方案1】:

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

     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
    }
    

    【讨论】:

    • 它是为 UIKit 设计的,不是吗?我使用 SwiftUI
    • 我编辑了答案以获得正确的 swiftUI 答案。
    • 这行得通,但 init() 只能在类/结构内工作,而不能在某些视图内工作。
    猜你喜欢
    • 2015-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-30
    • 1970-01-01
    • 2020-03-13
    • 1970-01-01
    相关资源
    最近更新 更多