Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added introspect for scroll view in TabBarView with PageTabViewStyle #117

Merged
merged 7 commits into from
Apr 17, 2022

Conversation

philprime
Copy link

I created an extension to introspect the horizontal scroll view in a TabBarView with tabViewStyle(PageTabViewStyle().

This tab view style is Apple's solution for pagination like UIPageViewController. Interesting enough, it actually uses a UICollectionView with an embedded UIScrollView.

I use this extension to disable horizontal scrolling (similar to scrollView.isScrollEnabled = false) so only programmatic navigation is possible. When horizontal scrolling is disabled, the collection view still bounces vertically, so both views are passed to the customize closure.

Example:

import SwiftUI
import Introspect

@main
struct DemoApp: App {

    @State private var selection = 1

    var body: some Scene {
        WindowGroup {
            TabView(selection: $selection) {
                Button("Go To Page 2", action: {
                    withAnimation {
                        selection = 2
                    }
                })
                .tag(1)
                Button("Go To Page 1", action: {
                    withAnimation {
                        selection = 1
                    }
                })
                .tag(2)
            }
            .tabViewStyle(PageTabViewStyle())
            .introspectPagedTabView { collectionView, scrollView in
                collectionView.isScrollEnabled = false
                scrollView.isScrollEnabled = false
            }
        }
    }
}

@bratxdy
Copy link

bratxdy commented May 20, 2021

Any chance we can get this into master and released? We were using .introspectScrollView previously on a TabView with PageTabViewStyle to get the scroll view to turn off vertical bouncing and it broke when updating to 0.1.1, likely by 6ddd059

@dannyhertz
Copy link

+1 can we get this merged?

@joshuakcockrell
Copy link

joshuakcockrell commented Feb 1, 2022

This is working great for me! Setting the collectionView or scrollView delegate methods breaks the TabBar though.. SwiftUI must be doing some magic behind the scenes with the delegates.

Copy link

@guillecom guillecom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested this and it works in ios 15.4

@SplittyDev
Copy link
Collaborator

SplittyDev commented Apr 15, 2022

@philprime thanks a lot for the PR and sorry for the late reply!
I'd love to merge this, could you add tests to make sure this works as intended on all platforms?

@philprime
Copy link
Author

@SplittyDev I think I can do that, but I need assistance on how to create Introspect tests cases -> how do you verify it works?

@SplittyDev
Copy link
Collaborator

SplittyDev commented Apr 16, 2022

@SplittyDev I think I can do that, but I need assistance on how to create Introspect tests cases -> how do you verify it works?

@philprime In the IntrospectTests directory we have AppKitTests.swift and UIKitTests.swift. If an introspection works for both, we write two separate tests for AppKit and UIKit, otherwise we just write one for the correct platform.

We create a view for the test (e.g. TabBarTestView / PageTabViewStyleTestView), where the target component is introspected and a function (called a spy) is called from within the introspection.

We then write the test itself, which renders the test view and passes an XCTestExpectation to the component as the spy function. Then we present the component using a small helper utility and verify that the spy was called.

One more important thing is that the components / introspections are properly tagged with @available annotations, here is an example from the ColorWellTestView:

@available(iOS 14.0, tvOS 13.0, macOS 11.0, *)
@available(tvOS, unavailable)
private struct ColorWellTestView: View { ... }

Platform availability information can usually be taken directly from the docs:
Apple Docs: https://developer.apple.com/documentation/swiftui/pagetabviewstyle

@pull-request-size pull-request-size bot added size/M and removed size/S labels Apr 17, 2022
@philprime
Copy link
Author

@SplittyDev I added a test case as you described it. Let me know if more changes are required.

(Sorry for the formatting commits/notifications, for some reason it kept being wrong 😂)

@SplittyDev
Copy link
Collaborator

@philprime good stuff, thanks a lot! The formatting commits aren't a problem, we'll squash-merge anyway :)

@SplittyDev SplittyDev merged commit 7ef0df6 into siteline:master Apr 17, 2022
@ichikmarev
Copy link

@SplittyDev Hi. This awesome PR was merged but seemed lost since it is not in 0.1.4.

@philprime
Copy link
Author

@ichikmarev actually the PR hasn't been released yet.

My commit is in the main branch with date April 17th, 2022, but the release 0.1.4 was published on January 26, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants