Go to file
cecipirotto f9579b657d
update Package.swift (#34)
2021-08-05 16:07:14 -03:00
.github base oss project created with https://github.com/xmartlabs/Swift-Framework-Template 2020-07-17 16:41:52 -03:00
Example Merge pull request #30 from xmartlabs/exampleChanges 2021-08-02 10:52:55 -03:00
PagerTabStrip some fixes and cleaning up code. (#12) 2021-07-13 12:07:40 -03:00
PagerTabStrip.xcodeproj remove Modifiers folder and category (#26) 2021-07-28 13:22:37 -03:00
PagerTabStrip.xcworkspace Pages scroll with options (#1) 2020-07-22 18:13:34 -03:00
Sources public code documented 2021-07-30 12:14:12 -03:00
Tests some fixes and cleaning up code. (#12) 2021-07-13 12:07:40 -03:00
.gitignore first commit 2020-07-17 16:41:19 -03:00
.travis.yml base oss project created with https://github.com/xmartlabs/Swift-Framework-Template 2020-07-17 16:41:52 -03:00
CHANGELOG.md base oss project created with https://github.com/xmartlabs/Swift-Framework-Template 2020-07-17 16:41:52 -03:00
Cartfile.private base oss project created with https://github.com/xmartlabs/Swift-Framework-Template 2020-07-17 16:41:52 -03:00
Cartfile.resolved base oss project created with https://github.com/xmartlabs/Swift-Framework-Template 2020-07-17 16:41:52 -03:00
LICENSE base oss project created with https://github.com/xmartlabs/Swift-Framework-Template 2020-07-17 16:41:52 -03:00
Package.swift update Package.swift (#34) 2021-08-05 16:07:14 -03:00
PagerTabStrip.podspec Add Package.swift and update Podspec (#14) 2021-07-23 16:23:47 -03:00
README.md public code documented 2021-07-30 12:14:12 -03:00

README.md

PagerTabStrip

Platform iOS Swift 5 compatible Carthage compatible CocoaPods compatible License: MIT

Made with ❤️ by Xmartlabs team. XLPagerTabStrip for SwiftUI!

Introduction

PagerTabStrip is the first pager view built in pure SwiftUI. It provides PagerTabStripView component to create interactive pager views which contains child views. It allows the user to switch between your views either by swiping or tapping a tab bar item.

Unlike Apple's TabView it provides:

  1. Flexible way to fully customize pager tab views.
  2. Each pagerTabItem view can be of different type.
  3. Bar that contains pager tab item is placed on top.
  4. Indicator view indicates selected child view.
  5. onPageAppear callback to easily trigger actions when page is selected.
  6. Ability to update pagerTabItem according to highlighted, selected, normal state.

..and we've planned many more functionalities, we have plans to support each one of the XLPagerTabStrip styles.

Usage

Creating a page view is super straightforward, you just need to place your custom tab views into a PagerTabStripView view and apply the pagerTabItem( _: ) modifier to each one to specify its navigation bar tab item.

import PagerTabStrip

struct MyPagerView: View {

    var body: some View {

        PagerTabStripView() {
            MyFirstView()
                .pagerTabItem {
                    TitleNavBarItem(title: "Tab 1")
                }
            MySecondView()
                .pagerTabItem {
                    TitleNavBarItem(title: "Tab 2")
                }
            if User.isLoggedIn {
                MyProfileView()
                    .pagerTabItem {
                        TitleNavBarItem(title: "Profile")
                    }
            }
        }

    }
}


To specify the initial selected page you can pass the selection init parameter.

struct MyPagerView: View {

    @State var selection = 1

    var body: some View {
        PagerTabStripView(selection: $selection) {
            MyFirstView()
                .pagerTabItem {
                    TitleNavBarItem(title: "Tab 1")
                }
            ...
            ..
            .
        }
    }
}

As you may've already noticed, everything is SwiftUI code, so you can update the child views according to SwiftUI state objects as shown above with if User.isLoggedIn.

Customize pager style

You have the ability to customize some aspects of the navigation bar and its indicator bar using the pagerTabStripViewStyle modifier. The customizable settings are:

  • Spacing between navigation bar items
  • Navigation bar height
  • Indicator bar height
  • Indicator bar color
struct PagerView: View {

	var body: some View {
		PagerTabStripView(selection: 1) {
			MyView()
				.pagerTabItem {
					TitleNavBarItem(title: "Tab 1")
				}
			AnotherView()
				.pagerTabItem {
					TitleNavBarItem(title: "Tab 2")
				}
			if User.isLoggedIn {
				ProfileView()
					.pagerTabItem {
						TitleNavBarItem(title: "Profile")
                    }
			}
		}
        	.pagerTabStripViewStyle(PagerTabViewStyle(tabItemSpacing: 0, tabItemHeight: 50, indicatorBarHeight: 2, indicatorBarColor: .gray))
	}
}

In this example, we add some settings like the tab bar height, indicator bar color and indicator bar height. Let's watch how it looks!

Navigation bar

The navigation bar supports custom tab items. You need to specify its appearance creating a struct that implements View protocol.

For simplicity, we are going to implement a nav bar item with only a title. You can find more examples in the example app.

struct TitleNavBarItem: View {
    let title: String

    var body: some View {
        VStack {
            Text(title)
                .foregroundColor(Color.gray)
                .font(.subheadline)
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .background(Color.white)
    }
}

Customize selected and highlighted items

You can define the style of your nav items when they are selected or highlighted by conforming PagerTabViewDelegate protocol in your nav item view.

In the following example we change the text and background color when the tab is highlighted and selected.

private class NavTabViewTheme: ObservableObject {
    @Published var textColor = Color.gray
    @Published var backgroundColor = Color.white
}

struct TitleNavBarItem: View, PagerTabViewDelegate {
    let title: String
    @ObservedObject fileprivate var theme = NavItemTheme()

    var body: some View {
        VStack {
            Text(title)
                .foregroundColor(theme.textColor)
                .font(.subheadline)
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .background(theme.backgroundColor)
    }

    func setState(state: PagerTabViewState) {
        switch state {
        case .selected:
            self.theme.textColor = .blue
            self.theme.backgroundColor = .lightGray
        case .highlighted:
            self.theme.textColor = .pink
        default:
            self.theme.textColor = .gray
            self.theme.backgroundColor = .white
        }
    }
}

onPageAppear modifier

onPageAppear callback allows you to trigger some action when a page view gets selected, either by scrolling to it or tapping its tab. This modifier is applied only to its associated page view.

struct PagerView: View {

    var body: some View {
        PagerTabStripView(selection: 1) {
            MyView(model: myViewModel)
                .pagerTabItem {
                    TitleNavBarItem(title: "Tab 1")
                }
                .onPageAppear {
                    model.reload()
                }
        }
        .pagerTabStripViewStyle(PagerTabViewStyle(tabItemSpacing: 0, tabItemHeight: 50, indicatorBarHeight: 2, indicatorBarColor: .gray))
    }
}

Examples

Follow these 3 steps to run Example project

  • Clone PagerTabStrip repo.
  • Open PagerTabStrip workspace.
  • Run the Example project.

Installation

CocoaPods

To install PagerTabStrip using CocoaPods, simply add the following line to your Podfile:

pod 'PagerTabStrip', '~> 1.0'

Carthage

To install PagerTabStrip using Carthage, simply add the following line to your Cartfile:

github "xmartlabs/PagerTabStrip" ~> 1.0

Requirements

  • iOS 14+
  • Xcode 12.X

Author

Getting involved

  • If you want to contribute please feel free to submit pull requests.
  • If you have a feature request please open an issue.
  • If you found a bug or need help please check older issues and threads on StackOverflow (Tag 'PagerTabStrip') before submitting an issue.

Before contribute check the CONTRIBUTING file for more info.

If you use PagerTabStrip in your app We would love to hear about it! Drop us a line on Twitter.