简体   繁体   English

使 ScrollView 中的滚动条在 SwiftUI 中始终可见

[英]Make scrollbar in ScrollView always visible in SwiftUI

I'm developing an application in SwiftUI.我正在用 SwiftUI 开发一个应用程序。 I have a horizontal scroll view which shows the scroll bar when the user taps on it.我有一个水平滚动视图,当用户点击它时会显示滚动条。 As this app is targeted more towards older users, I want to continually display the scrollbar.由于这个应用程序更多地针对老年用户,我想不断地显示滚动条。 I couldn't find anything in the Apple documentation on how to enable this.我在 Apple 文档中找不到有关如何启用此功能的任何内容。 There's an attribute called showIndicators which is true by default and enables the scrollbar by tapping but doesn't continually display the scrollbar.有一个名为showIndicators的属性默认为 true,通过点击启用滚动条,但不会持续显示滚动条。 This is a simple code snippet of my ScrollView:这是我的 ScrollView 的一个简单代码片段:

 var body: some View {
    ScrollView(.horizontal) {
        HStack(spacing: 4) {
        // other stuff in the scroll view
    }   
 }

I don't think you can make the scroll bar permanently visible.我认为您不能使滚动条永久可见。 One thing that might solve your problem is to flash the scroll bar once when the user is first introduced to the collection view to notify them that this view can be scrolled.可能会解决您的问题的一件事是,当用户第一次被介绍到集合视图时,让滚动条闪烁一次,以通知他们该视图可以滚动。 In UIKit we have this UIScrollView method:在 UIKit 我们有这个 UIScrollView 方法:

scrollView.flashScrollIndicators()

I don't know if SwiftUI already has something like that.我不知道 SwiftUI 是否已经有了类似的东西。

After an hour of research, I found this post: Get the current position of ScrollView in SwiftUI?经过一个小时的研究,我找到了这篇文章: 在 SwiftUI 中获取 ScrollView 的当前位置?

It let's me get the current position of a scroll view and I can then move a drawn rectangle (which looks like a scroll bar) according to the tracked position of the scrollview.它让我获取滚动视图的当前位置,然后我可以根据滚动视图的跟踪位置移动绘制的矩形(看起来像滚动条)。

you must make slider and rotate as vertically ,then you can use with variable at on change slider change value , like this:您必须制作滑块并垂直旋转,然后您可以在更改滑块更改值时使用变量,如下所示:

 ScrollViewReader { reader in
                        ScrollView(showsIndicators: false){

                             withAnimation(Animation.easeInOut(duration: 
                                           4).delay(3)) {
                                                    
                              reader.scrollTo(selectedIndex, anchor: 
                                              UnitPoint.center)
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM