EVTopTabBar 是一个自定义的 UIPageViewController,其中页面控制器位于顶部。它目前支持最多 4 个标签。
要运行示例项目,克隆仓库,然后在 Example 目录中运行 pod install
。
EVTopTabBar 可通过 CocoaPods 使用。要安装它,只需将以下行添加到 Podfile 中
pod "EVTopTabBar"
另外包括
use_frameworks!
import EVTopTabBar
实现 EVTabBar 协议并遵守它(示例项目有详细的实现)。为了遵守协议,请声明以下内容
var pageController = UIPageViewController(transitionStyle: .Scroll, navigationOrientation: .Horizontal, options: nil)
//need to instantiate the topTabBar itself, later we will implement the delegate method
//enum .two, .three, .four correspond to the number of tabs you wish to display
var topTabBar: EVPageViewTopTabBar = EVPageViewTopTabBar(for: .four)
//array of view controllers that will be controlled by the page view controller
var subviewControllers: [UIViewController] = []
//image from the sample project can use any UIImage you want, this shadow is what is displayed under the tab bar.
var shadowView = UIImageView(image: UIImage(imageLiteral: "filter-background-image"))
现在在您的视图控制器中实现 EVPageViewTopTabBarDelegate 方法 willSelectViewControllerAtIndex
。此方法允许视图控制器切换,并提供空间执行其他操作。
extension ViewController: EVPageViewTopTabBarDelegate {
//delegate method
func willSelectViewControllerAtIndex(index: Int, direction: UIPageViewControllerNavigationDirection) {
//required line that changes the displayed view controller
pageController.setViewControllers([self.subviewControllers[index]], direction: direction, animated: true, completion: nil)
//add whatever code you wish to occur on the transition!
}
}
接下来,将需要调用 setupPageView()
和 setupConstraints()
方法以设置页面布局。您可以通过覆盖这两个方法来创建更自定义的应用程序。
最后,您将需要设置之前声明的 topTabBar
//attributes of the topTabBar you can set
topTabBar.fontColors = (selectedColor: UIColor.grayColor(), unselectedColor: UIColor.lightGrayColor())
topTabBar.rightButtonText = "Events"
topTabBar.leftButtonText = "Contacts"
topTabBar.labelFont = UIFont(name: ".SFUIText-Regular", size: 11)!
topTabBar.indicatorViewColor = UIColor.blueColor()
topTabBar.backgroundColor = UIColor.whiteColor()
//ensure you set the topTabBar's delegate
topTabBar.delegate = self
//add view controllers you wish to display
let firstVC = FirstViewController(nibName:"FirstViewController", bundle: nil)
let secondVC = SecondViewController(nibName:"SecondViewController", bundle: nil)
subviewControllers = [firstVC, secondVC]
setupPageView()
setupConstraints()
setupPageView()
设置视图控制器中的 UI,包括 UIPageViewController、shadowView 以及要在页面视图中显示的视图控制器。setupConstraints()
设置视图控制器的初始约束willSelectViewControllerAtIndex(index: Int, direction: UIPageViewControllerNavigationDirection)
控制页面视图内部的视图控制器切换,并提供空间在转换时执行其他操作。Eric Vennaro,[email protected],博客
EVTopTabBar 在 MIT 许可证 之下。有关更多信息,请参阅 LICENSE 文件。
版权 © 2016 年至现在 Eric Vennaro。