SwiftUI is a powerful and lightweight UI component framework for iOS and OS X written in pure Swift and based on the paper and events concept. It is simple and flexible, easy to use and extend to achieve the best user interface.
One of my frustrations is that Apple’s default iOS applications are somewhat ‘bare bones’ in their design. They are not as elegant as I would like them to be, for example, I have often wished that my default text messaging application had a quick page-turn button, so I could flip through my messages without having to reach for the volume button.
Today, I want to share with you something I’ve been working on for about 6 months. It’s supposed to be an elegant pager view for iOS, but I’ve created it in the SwiftUI framework instead of using UIKit directly.
The Xmartlabs team created this. Introducing the XLPagerTabStrip for SwiftUI!
Introduction
PagerTabStripView is the first pure SwiftUI pager view. It comes with a component for creating interactive pager views with child views. Swiping or touching a tab bar item enables the user to move between your views.
Unlike Apple’s TabView, it has the following features:
- Pager tab displays may be completely customized in a variety of ways.
- Different types of pagerTabItem views are possible.
- On top of the pager tab item is a bar with the pager tab item.
- The indicator view shows the child view that has been chosen.
- When a page is chosen, use the onPageAppear callback to quickly initiate actions.
- Ability to update pagerTabItem based on whether it is highlighted, chosen, or normal.
We have plans to support each of the XLPagerTabStrip designs, and we have many more features planned.
Usage
To make a page view, just put your custom tab views in a PagerTabStripView view and use the pagerTabItem( _: ) modifier to define the navigation bar tab item for each one.
PagerTabStripView struct MyPagerView: View var body: some View PagerTabStripView() MyFirstView() import PagerTabStripView struct MyPagerView: View var body: some View PagerTabStripView() MyFirstView() MySecondView() pagerTabItem TitleNavBarItem(title: “Tab 1”) pagerTabItem if User.isLoggedIn MyProfileView(), pagerTabItem TitleNavBarItem(title: “Tab 2”) pagerTabItem TitleNavBarItem(title: “Tab 2”) pagerTabItem TitleNavBarItem(title: “Tab 2”) pagerTabItem TitleNavBarItem(title pagerTabItem TitleNavBarItem(title: “Profile”) navBarItem(title: “Profile”) navBarItem(title: “Profile”) navBarItem(title: “Profile”) navBarItem(title: “Profile”) nav
The selection init option may be used to define the first chosen page.
@State var selection = 1 var body: some struct MyPagerView: View MyFirstView() is a PagerTabStripView(selection: $selection) view. pagerTabItem TitleNavBarItem(title: “Tab 1”)………………………
Everything is SwiftUI code, as you may have seen, thus you can change the child views based on SwiftUI state objects, as demonstrated above with if User.isLoggedIn.
Customize the pager’s appearance.
PagerTabstripView has three alternative methods of displaying the views. Using the pagerTabStripViewStyle modification, you may pick it and modify certain features of each one.
Typical fashion
This is most certainly the most popular pager configuration. The following options may be changed:
- The distance between the elements in the navigation bar
- Height of the navigation bar
- Height of the indicator bar
- Color of the indicator bar
var body: some struct PagerView: View View PagerTabStripView (PagerTabStripView) (PagerTabStripView (selection: 1) MyView() is a function that returns a view. TitleNavBarItem(title: “Tab 1”) pagerTabItem AnotherView() is a function that returns another view. titleNavBarItem(title: “Tab 2”) pagerTabItem ProfileView().pagerTabItem TitleNavBarItem(title: “Profile”) if User.isLoggedIn pagerTabStripViewStyle(.normal(indicatorBarColor:.gray, tabItemSpacing: 0, tabItemHeight: 50)) pagerTabStripViewStyle(.normal(indicatorBarColor:.gray, tabItemSpacing: 0, tabItemHeight: 50)) pagerTabStripViewStyle(.normal(indicatorBar
In this example, we set the height of the tab bar, the color of the indicator bar, and the height of the indicator bar. Let’s see how it turns out!
Bar style
This design simply displays a bar that shows which view controller is currently active. The following options may be changed:
- The distance between the elements in the navigation bar
- Height of the indicator bar
- Color of the indicator bar
segmented design
A Segmented Picker is used in this design to identify which view is being shown. You may specify the chosen color, padding, and whether or not it should be shown in the toolbar.
Bar of navigation
Custom tab items may be added to the navigation bar. You must create a struct that implements the View protocol to define its appearance.
We’ll use a nav bar item with just a title for simplicity’s sake. More examples may be found in the sample app.
View struct TitleNavBarItem: struct TitleNavBarItem: struct TitleNavBarItem: struct TitleNavBarItem: struct body: some string var body: some string var body: some string var body Text(title).foregroundColor(Color.gray).font(.subheadline) View VStack .background(Color.white).frame(maxWidth:.infinity, maxHeight:.infinity)
Customize the things you’ve chosen and highlighted.
By adhering to the PagerTabViewDelegate protocol in your nav item view, you may specify the style of your nav items when they are chosen or highlighted.
When the tab is highlighted and chosen, we alter the text and background color in the following example.
@Published var textColor = Color.gray @Published var backgroundColor = Color.white private class NavTabViewTheme: ObservableObject @Published var textColor = Color.gray @Published var backgroundColor = Color.white let title: String @ObservedObject fileprivate var theme = NavItemTheme struct TitleNavBarItem: View, PagerTabViewDelegate () body variable: some View VStack is an acronym for Virtual Stack. .foregroundColor(theme.textColor).font(.subheadline) Text(title).foregroundColor(theme.textColor) .background(theme.backgroundColor).frame(maxWidth:.infinity, maxHeight:.infinity) switch state case.selected: self.theme.textColor =.blue self.theme.backgroundColor =.blue func setState(state: PagerTabViewState) func setState(state: PagerTabViewState) func setState(state: PagerTabViewState) func setState(state: PagerTabViewState) func setState .light color self.theme.textColor =.pink; self.theme.textColor =.pink; self.theme.textColor =.pink; self.theme.textColor self.theme.textColor =.gray self.theme.backgroundColor =.white self.theme.textColor =.gray
Modifier onPageAppear
When a page view is chosen, either by scrolling to it or clicking its tab, the onPageAppear callback enables you to do some action. This modification is only applied to the page view with which it is connected.
var body: some struct PagerView: View View PagerTabStripView (PagerTabStripView) (PagerTabStripView (selection: 1) MyView(model: myViewModel) is a class that represents a view. TitleNavBarItem(title: “Tab 1”) pagerTabItem model.reload() if onPageAppear is true. pagerTabStripViewStyle(.normal(indicatorBarHeight: 2, indicatorBarColor:.gray, tabItemSpacing: 0, tabItemHeight: 50)) pagerTabStripViewStyle(.normal(indicatorBarHeight: 2, indicatorBarColor:.gray, tabItemSpacing: 0, tabItemHeight: 50)) pagerTabStripViewStyle(.normal(indic
Examples
To run the Example project, follow these three steps.
- PagerTabStripView is a clone of the PagerTabStripView repo.
- Open the workspace PagerTabStripView.
- Use the Example project to get started.
Installation
CocoaPods
Simply add the following line to your Podfile to install PagerTabStripView using CocoaPods:
2.0′ “> pod ‘PagerTabStripView’, ‘~> 2.0’
Carthage
To use Carthage to install PagerTabStripView, just add the following line to your Cartfile:
2.0 “> github “xmartlabs/PagerTabStripView” ~> 2.0
Requirements
Author
Participating in the process
- Please feel free to send pull requests if you wish to help.
- Please file an issue if you have a feature request.
- Please review previous problems and discussions on StackOverflow (Tag ‘PagerTabStripView’) before filing an issue if you discovered a bug or need assistance.
Check the CONTRIBUTING file for additional information before contributing.
We’d love to hear about how you used PagerTabStripView in your app! Send us a message on Twitter.
GitHub
https://github.com/xmartlabs/PagerTabStripView
Related Tag
- swiftui swipe between views