All Projects → Amzd → ScrollViewProxy

Amzd / ScrollViewProxy

Licence: MIT license
ScrollViewProxy for SwiftUI on iOS 13 and up

Programming Languages

swift
15916 projects
ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to ScrollViewProxy

About Swiftui
Gathering all info published, both by Apple and by others, about new framework SwiftUI.
Stars: ✭ 5,954 (+4310.37%)
Mutual labels:  apple, swiftui
Swiftvalidators
String (and more) validation for iOS
Stars: ✭ 226 (+67.41%)
Mutual labels:  apple, swift-package-manager
Open Source Ios Apps
📱 Collaborative List of Open-Source iOS Apps
Stars: ✭ 28,826 (+21252.59%)
Mutual labels:  apple, swiftui
SwiftUIFormValidator
Declarative form validator for SwiftUI.
Stars: ✭ 34 (-74.81%)
Mutual labels:  apple, swiftui
ResponderChain
Cross-platform first responder handling without subclassing views or making custom ViewRepresentables in SwiftUI. Similar to FocusState but for iOS 13+
Stars: ✭ 69 (-48.89%)
Mutual labels:  swift-package-manager, swiftui
TermiNetwork
🌏 A zero-dependency networking solution for building modern and secure iOS, watchOS, macOS and tvOS applications.
Stars: ✭ 80 (-40.74%)
Mutual labels:  apple, swiftui
Watchshaker
Simple motion detector for ⌚️ (watchOS) shake gesture.
Stars: ✭ 184 (+36.3%)
Mutual labels:  apple, swift-package-manager
Containercontroller
UI Component. This is a copy swipe-panel from app: Apple Maps, Stocks. Swift version
Stars: ✭ 273 (+102.22%)
Mutual labels:  swift-package-manager, scrollview
SwiftCurrent
A library for managing complex workflows in Swift
Stars: ✭ 286 (+111.85%)
Mutual labels:  swift-package-manager, swiftui
SwiftDown
📦 A themable markdown editor component for your SwiftUI apps.
Stars: ✭ 203 (+50.37%)
Mutual labels:  swift-package-manager, swiftui
SwiftUI-Tooltip
Easy Tooltip for your SwiftUI Project
Stars: ✭ 130 (-3.7%)
Mutual labels:  apple, swiftui
ConsoleUI
Rasterize SwiftUI views to images from macOS terminal
Stars: ✭ 52 (-61.48%)
Mutual labels:  swift-package-manager, swiftui
aprenda-swift
Uma lista de conteúdos para você aprender Swift
Stars: ✭ 429 (+217.78%)
Mutual labels:  apple, swiftui
concurrency-kit
🚄 Concurrency abstractions framework for Apple Platforms [Task, Atomic, Lock, Operation, etc.].
Stars: ✭ 17 (-87.41%)
Mutual labels:  apple, swift-package-manager
Swiftuix
Extensions and additions to the standard SwiftUI library.
Stars: ✭ 4,087 (+2927.41%)
Mutual labels:  swift-package-manager, swiftui
Statusalert
Display Apple system-like self-hiding status alerts. It is well suited for notifying user without interrupting user flow in iOS-like way.
Stars: ✭ 809 (+499.26%)
Mutual labels:  apple, swift-package-manager
Laden
SwiftUI loading indicator view
Stars: ✭ 23 (-82.96%)
Mutual labels:  swift-package-manager, swiftui
CurrencyText
Currency text field formatter available for UIKit and SwiftUI 💶✏️
Stars: ✭ 124 (-8.15%)
Mutual labels:  swift-package-manager, swiftui
Wwdc
You don't have the time to watch all the WWDC session videos yourself? No problem me and many contributors extracted the gist for you 🥳
Stars: ✭ 2,561 (+1797.04%)
Mutual labels:  apple, swiftui
SwiftUI-DesignCode
 SwiftUI-DesignCode is some examples in the process of learning swiftUI 2.0
Stars: ✭ 185 (+37.04%)
Mutual labels:  scrollview, swiftui

As of June 22 2020 this is included in the SwiftUI 2 beta. https://developer.apple.com/documentation/swiftui/scrollviewproxy

The Apple implementation uses just .id(_:) and I had update issues with that where Views with an id sometimes won't update when their ObservedObject changed. Maybe this has been fixed in the new SwiftUI 2 beta.

Also the Apple implementation only supports iOS 14 so I think this repo is still useful for backwards compatibility.

Note: An important difference between this library and Apples implementation is that the ScrollViewReader goes inside the ScrollView. If you place the ScrollViewReader around the ScrollView the scrollTo function will not scroll to the correct location (due to its coordinateSpace not being part of the scrolling content). I considered fixing this to align with Apple but that would break backwards compatibility with projects already using ScrollViewProxy.

Maybe it's possible to detect if we're inside or outside of a ScrollView in the reader but then how do we handle nested ScrollViews? If you have any ideas please open an Issue/PR or email me.

ScrollViewProxy

Adds ScrollViewReader and ScrollViewProxy that help you scroll to locations in a ScrollView

To get a ScrollViewProxy you can either use the conveinience init on ScrollView

ScrollView { proxy in
    ...
}

or add a ScrollViewReader to any View that creates a UIScrollView under the hood

List {
    ScrollViewReader { proxy in
        ...
    }
}

The ScrollViewProxy currently has one variable and two functions you can call

/// A publisher that publishes changes to the scroll views offset
public var offset: OffsetPublisher

/// Scrolls to an edge or corner
public func scrollTo(_ alignment: Alignment, animated: Bool = true)

/// Scrolls the view with ID to an edge or corner
public func scrollTo(_ id: ID, alignment: Alignment = .top, animated: Bool = true)

To use the scroll to ID function you have to add an ID to the view you want to scroll to

ScrollView { proxy in
    HStack { ... }
        .scrollId("someId")
}

This is the only part that is different from the SwiftUI 2.0 implementation because I don't know how to access Views by ID from the .id(_:) function

Example

Everything put together in an example

struct ScrollViewProxyExample: View {
    
    @State var randomInt = Int.random(in: 0..<200)
    @State var proxy: ScrollViewProxy? = nil
    @State var offset: CGPoint = .zero
    
    var body: some View {
        // GeometryReader for safeAreaInsets on Sticky View
        GeometryReader { geometry in 
            VStack {
                ScrollView { proxy in
                    Text("Sticky View")
                        .background(Color.white)
                        .onReceive(proxy.offset) { self.offset = $0 }
                        .offset(x: offset.x, y: offset.y + geometry.safeAreaInsets.top)
                        .zIndex(1)
                    
                    VStack(spacing: 20) {
                        ForEach(0..<200) { index in
                            HStack {
                                Spacer()
                                Text("Item: \(index)").font(.title)
                                Spacer()
                            }.scrollId(index)
                        }
                    }
                    .zIndex(0)
                    .onAppear {
                        self.proxy = proxy
                    }
                }
                HStack {
                    Button(action: {
                        self.proxy?.scrollTo(self.randomInt, alignment: .center)
                        self.randomInt = Int.random(in: 0..<200)
                    }, label: {
                        Text("Go to \(self.randomInt)")
                    })
                    Spacer()
                    Button(action: { self.proxy?.scrollTo(.bottom) }, label: {
                        Text("Bottom")
                    })
                    Spacer()
                    Button(action: { self.proxy?.scrollTo(.center) }, label: {
                        Text("Center")
                    })
                }.padding()
            }
        }
    }
}

Deintegrate

Want to drop iOS 13 support and move to the SwiftUI 2.0 version?

  1. Remove the Package
  2. Add this extension:
extension View {
    public func scrollId<ID: Hashable>(_ id: ID) -> some View {
        id(id)
    } 
}

(Or replace all .scrollId with .id)

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].