DTPagerController
这是一个用 Swift 编写的 iOS 控件。DTPagerController 简单易用,易于定制。
屏幕截图
默认的分段控制器
自定义的分段控制器
用法
DTPagerController 非常容易使用。要在页面控制器内显示两个视图控制器,您只需这样做这么多的代码。
let viewController1 = ViewController()
let viewController2 = ViewController()
let pagerController = DTPagerController(viewControllers: [viewController1, viewController2])
更新页面索引
有三种不同的方法可以程序化地更新所选页面索引。
// Update selected page index with animation
pagerController.selectedPageIndex = 1
// Update selected page index with animation
pagerController.setSelectedPageIndex(1, animated: true)
// Update selected page index without animation
pagerController.setSelectedPageIndex(1, animated: false)
自定义 UI
DTPagerController也支持自定义,以便您实现自己的UI。
// Change the height of segmented control
pagerController.preferredSegmentedControlHeight = 60
// Change normal font of each segmented control
pagerController.font = UIFont.customFont(ofSize: 15)
// Change selected font of each segmented control
pagerController.selectedFont = UIFont.boldCustomFont(ofSize: 15)
// Change normal text color of each segmented control
pagerController.textColor = UIColor.black
// Change selected text color of each segmented control
pagerController.selectedTextColor = UIColor.red
// Change scroll indicator height
pagerController.perferredScrollIndicatorHeight = 3
自定义分段控制
从版本 2.0.0 开始,DTPagerController支持自定义分段控制。因此,您可以选择不使用默认的 DTSegmentedControl,而是提供自己的分段控制或任何可用的第三方分段控制库。您需要做的是使您的自定义UIControl符合 DTSegmentedControlProtocol。例如,如样例项目所示,通过使用扩展使 HMSegmentedControl 符合 DTSegmentedControlProtocol。
extension HMSegmentedControl: DTSegmentedControlProtocol {
public func setImage(_ image: UIImage?, forSegmentAt segment: Int) {
// Custom page control does not support
}
public func setTitle(_ title: String?, forSegmentAt segment: Int) {
// Custom page control does not support
}
public func setTitleTextAttributes(_ attributes: [AnyHashable : Any]?, for state: UIControlState) {
if state == UIControlState.normal {
titleTextAttributes = attributes
}
else if state == UIControlState.selected {
selectedTitleTextAttributes = attributes
}
}
}
随后,我们使用自定义分段控制创建新的页面控制器
init(viewControllers controllers: [UIViewController]) {
let segmentedControl = HMSegmentedControl(sectionTitles: ["Page 1", "Page 2", "Page 3"])
super.init(viewControllers: controllers, pageSegmentedControl: segmentedControl!)
}
使用自定义分段控制时,建议重写/查看以下方法来自定义各个分段的行为和外观
// Setup custom segmented control
func setUpSegmentedControl(viewControllers: [UIViewController])
// Update a custom appearance for segment
func updateAppearanceForSegmentedItem(at index: Int)
// Update a custom scroll indicator if exists
func updateScrollIndicator(with offsetRatio: CGFloat, scrollView: UIScrollView)
// Setup custom scroll indicator
func setUpScrollIndicator()
// Manually update segment title
func setTitle(_ title: String?, forSegmentAt segment: Int)
// Manually update segment image
func setImage(_ image: UIImage?, forSegmentAt segment: Int)
示例
要运行示例项目,请首先克隆存储库,然后从示例目录运行 pod install
。
需求
需要iOS 9.0以上版本
安装
CocoaPods
只需在 Podfile 中添加以下一行代码
Swift 5
pod 'DTPagerController'
Swift 4.2
pod 'DTPagerController', '~> 2.0.4'
Swift 包管理器
DTPagerController
从版本 3.0.2
开始支持 SPM。请将以下内容添加到 Package.swift
的依赖项中
.package(url: "https://github.com/tungvoduc/DTPagerController", from: "version")
作者
Pham Trong Than, [email protected]
许可证
DTPagerController 在 MIT 许可下可用。有关更多信息,请参阅 LICENSE 文件。
反馈与请求
- 如果您发现了一个错误,提出建议或只是需要一些帮助,请提交一个问题。
- 您也可以通过 [email protected] 联系我。