A lightweight paging view solution for SwiftUI

I’ve been using the iOS 11 Paging view for a few weeks as a replacement for the old navigation bar as a way to use as little as possible on the screen at once. It’s a fantastic way to navigate over several pages quickly, but it uses a lot of space and has a performance impact. I’m still trying to find a good solution to the problem—in the meantime, I thought I’d share my current solution and see if anyone else has any ideas.

When working in iOS and using an iOS or UIKit based app (Swift or otherwise), you often find yourself wanting to access a particular page of content. But when you go to access that page, you find yourself in a page controller, and since you want to go to a different page, you need to click the back button. What if there was a solution to this problem? A lightweight paging view (LWV) that lets you navigate through a set of pages of content without having to do that cumbersome back button click.

The most common paging solution for iOS UIs is a UITableView . While they provide a very flexible way to show a lot of information in a page-based interface, they tend to be a bit weighty, bulky and prone to memory leaks. For these reasons, I was recently experimenting with ways to create a robust, lightweight, fast scrolling paging view for iOS.

A-lightweight-paging-view-solution-for-SwiftUI

The First Steps | Personalization | Setup

1630627479_177_A-lightweight-paging-view-solution-for-SwiftUI

art-demo onboarding-demo book-preview

Getting Started

The fundamentals

It’s as simple as this to use Pages:

Pages struct import @State var index: Int = 0 WelcomeView: View body variable: some View Pages ($index)(currentPage: $index)(currentPage: $index)(currentPage: $index)(current { Text(“Welcome! This is the first page “( a text (“This is Page 2”) Circle() / The 4th page is a Circle Text(“…and this is Page 3”) Text(“…and this is Page 3”) Text(“…and this is Page 3”) Text(“…and this is Page 3”) Text(“…and this is Page 3”)

Pages with dynamic content may also be used:

Pages to Import var model: String struct Car struct CarsView: This is a view of cars. let vehicles = [Car(model: “Ferrari”), Car(model: “Ford”)] @State Int = 0 for index body variable: some View ModelPages(cars, currentPage: $index, currentPage: $index, currentPage: $index, currentPage: $index, currentPage: text(“The (pageIndex) car is a (car.model)”), pageIndex, car in Text(“The (pageIndex) car is a (car.model)”), pageIndex, car in Text(“The (pageIndex) car is a padding(50) padding(50) padding(50) padding(50) padding(50 foregroundColor(.white) is the color of the foreground. (Color.blue) as a backdrop. cornerRadius(10)

How does it work?

While Pages utilizes a UIPageViewController behind the hood, it uses a function builder to get a SwiftUI feel. The current maximum for adding pages in a static manner using the Pages view is ten, same like in VStack or HStack. Use a ModelPages instead if more are required. The Pages view will take up all of the space allocated to it.

Note that the Pages view requires several pages. Otherwise, the compiler considers the contents of Pages to be a closure.

Customization

Pages may be modified in the following ways:

  • navigation Whether to paginate horizontally or vertically is determined on the orientation. The default value is.horizontal.

a few pages (navigationOrientation: .vertical) Text(“Page 1”), Text(“Page 2”), Text(“Page 3”), Text(“Page 4”), Text(“Page 5”), Text(“

  • On page turn, choose whether to execute a page curl or a scroll effect. In the GIFs above, the first two instances utilize a scroll effect, while the final one uses page curl. .scroll is the default.

Navigation (pages) (.pageCurl,.vertical,.vertical,.vertical,.vertical,.vertical,.vertical,.vertical,.vertical,.vertical,.vertical Text(“Page 1”), Text(“Page 2”), Text(“Page 3”), Text(“Page 4”), Text(“Page 5”), Text(“

  • If the user attempts to scroll beyond the amount of pages, a bounce effect will be applied. True is the default value.

Navigation (pages) bounce: false,.vertical, transitionStyle:.pageCurl, Orientation:.vertical) Message (“Page 1”) a text(“Page 2”) a text(“Page 2”) a text(“Page

  • wrap: Whether or not to wrap the pages when a user attempts to go on to the next page after leaving the previous one. Similarly, when the user goes to the previous page of the first page, whether to proceed to the final page. The default value is false.

Navigation (pages) Transition:.vertical, orientation:.vertical, orientation:.vertical, orientation:.vertical, orientation:. bounce: false, wrap: true ) Style:.pageCurl, bounce: false, wrap: true ) Message (“Page 1”) a text(“Page 2”) a text(“Page 2”) a text(“Page

  • hasControl: Whether or not to show a page control. True is the default value.

Navigation (pages) bounce: false, wrap: true, hasControl: false) Orientation:.vertical, transitionStyle:.pageCurl, bounce: false, wrap: true, hasControl: false) Message (“Page 1”) a text(“Page 2”) a text(“Page 2”) a text(“Page

  • If you wish to adjust it, you may utilize a user-defined control. The traditional iOS page control will be utilized if this parameter is not supplied and hasControl is true. UIPageControl must be followed while controlling notes.

Navigation (pages) TransitionStyle:.page, Orientation:.vertical Curl, bounce: false, wrap: true, MyPageControl() control) Message (“Page 1”) a text(“Page 2”) a text(“Page 2”) a text(“Page

  • control Alignment: Where should the page control be placed inside Pages? .bottom is the default value.

Navigation (pages) Transition:.vertical, orientation:.vertical, orientation:.vertical, orientation:.vertical, orientation:. control,.pageCurl, bounce: false, wrap: true, style:.pageCurl .topLeading ) Alignment Message (“Page 1”) a text(“Page 2”) a text(“Page 2”) a text(“Page

FAQ

Demos

The demo repo contains all of the demonstrations shown in the GIF.

Installation

Using the Swift Package Manager, you can get Pages:

Using Xcode 11, go to File -> Swift Packages -> Add Package Dependency and enter https://github.com/nachonavarro/Pages

Executing the tests

To run the tests, hit U after selecting an iPhone target in Xcode. Alternatively, use the terminal to execute xcodebuild test -destination ‘name=iPhone 11’ -scheme ‘Pages’.

Requirements

TODOs

  • Unit and UI tests should be included.
  • Conditional clauses should be added to the function constructor.
  • Is it possible to combine ModelPages and Pages into a single view?

Contributing

Contribute to Pages if you’d like!

  1. Pages that have been forked
  2. Create a feature branch using the modifications you’ve made.
  3. Make a pull request

License

The MIT license governs the use of Pages. For additional information, see the LICENSE.

GitHub

https://github.com/nachonavarro/Pages

Paged views are lately becoming quite popular, and I’ve found myself using them all the time to display related information on an iOS app. However, there are always cases where I’d like to display content that is less relevant. Therefore, I’ve developed a lightweight paging view that can be easily deployed on any iOS app.. Read more about swiftui infinite horizontal scroll and let us know what you think.

Related Tags

  • swiftui vertical paging
  • swiftui pagination
  • swiftui lazyvstack paging
  • scrollview proxy swiftui
  • swiftui lazyvgrid paging