简体   繁体   中英

Make scrollbar in ScrollView always visible in SwiftUI

I'm developing an application in 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. There's an attribute called showIndicators which is true by default and enables the scrollbar by tapping but doesn't continually display the scrollbar. This is a simple code snippet of my 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:

scrollView.flashScrollIndicators()

I don't know if SwiftUI already has something like that.

After an hour of research, I found this post: Get the current position of ScrollView in SwiftUI?

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)
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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