测试已测试 | ✗ |
Lang语言 | SwiftSwift |
许可 | MIT |
发布上次发布 | 2017年2月 |
SwiftSwift 版本 | 3.0 |
SPM支持 SPM | ✗ |
由 Sahil Mahajan 维护。
SMSwipeableTabView 是一种自定义控件,它结合了 UIPageViewController 和可滚动的标签栏。这与 Android 中的带有标签布局的滑动视图类似。可以添加任意数量的标签和可滑动视图。用户可以完全自定义该控件。
要运行示例项目,请克隆存储库,然后首先从示例目录中运行 pod install
。
此库与 iOS 版本 8.0 及以上兼容。它使用 Swift 编写。
(无自定义)
(查看提供的示例)
我们需要在我们的 viewController 中添加以下代码以实现此控件。
//Add the title bar elements as an Array of String
swipeableView.titleBarDataSource = titleBarDataSource //Array of Button Titles like ["Punjab", "Karnataka", "Mumbai"]
//Assign your viewcontroller as delegate to load the Viewcontroller
swipeableView.delegate = self
//Set the View Frame (64.0 is 44.0(NavigationBar Height) + 20.0(StatusBar Height))
swipeableView.viewFrame = CGRect(x: 0.0, y: 64.0, width: UIScreen.mainScreen().bounds.width, height: UIScreen.mainScreen().bounds.height-64.0)
//Then add the view controller on the current view.
self.addChildViewController(swipeableView)
self.view.addSubview(swipeableView.view)
swipeableView.didMoveToParentViewController(self)
每当您点击任何部分按钮或在控制器中滑动页面时,代理方法将返回要加载的下一个 viewcontroller。
func didLoadViewControllerAtIndex(index: Int) -> UIViewController {
//We can implement switch case with the index Parameter and load new controller at every new index. Or we can load the same list view with different datasource.
let listVC = UIViewController()
listVC.backgroundColor = UIColor.red
listVC.dataSource = anyArray[index] // This will be an array of arrays or we need to set our dataSource of every different controller.
return listVC
}
我们可以自定义此视图中的每个控件。只需传递一个包含所需字段的字典。
注意:在设置所有属性后,设置 swipeableViewController 的框架。
例如:如果您想更改顶部栏的背景色,只需添加以下代码
swipeableView.segmentBarAttributes = [SMBackgroundColorAttribute : UIColor.lightGray]
如果您想更改选择栏(位于部分按钮下的栏)的背景色并设置选择栏的 Alpha,只需添加以下代码
swipeableView.selectionBarAttributes = [
SMBackgroundColorAttribute : UIColor.green,
SMAlphaAttribute : 0.8
]
您还可以更新部分按钮,只需为按钮添加属性
// Setting Font And BackgroundColor of Button
swipeableView.buttonAttributes = [
SMBackgroundColorAttribute : UIColor.green,
SMAlphaAttribute : 0.8,
SMFontAttribute : UIFont(name: "HelveticaNeue-Medium", size: 13.0)
]
如果您想在按钮中添加图像而不是标题,您可以在字典中轻松设置正常图像和突出显示图像属性,例如:
// Setting Font And BackgroundColor of Button
// Here for Normal and Highlighted Images we need to send the imageName array
swipeableView.buttonAttributes = [
SMBackgroundColorAttribute : UIColor.clear,
SMAlphaAttribute : 0.8,
SMButtonHideTitleAttribute : true,
SMButtonNormalImagesAttribute :["image_name1", "image_name2"] as [String]),
SMButtonHighlightedImagesAttribute : ["high_image_name1", "high_image_name2"] as [String])
]
您可以在字典中添加以下属性键:
SMFontAttribute // Set UIFont insatance
SMForegroundColorAttribute // Set UIColor instance (e.g. : Button Title Label ForegroundColor)
SMBackgroundColorAttribute // Set UIColor instance
SMAlphaAttribute // Set CGFloat value
SMBackgroundImageAttribute // Set UIImage instance
SMButtonNormalImageAttribute // Set UIImage instance
SMButtonHighlightedImageAttribute // Set UIImage instance
SMButtonHideTitleAttribute // Set Bool instance
selectionBar的宽度具有很强的可定制性。我们可以将其宽度设置为固定或可变。
swipeableView.buttonWidth = 60.0
类似地,您可以使用以下方式更改selectionBar的高度:
swipeableView.selectionBarHeight = 2.0 //For thin line
要更改segmentBar的高度,请使用以下代码行:
swipeableView.segementBarHeight = 50.0 //Default is 44.0
可以使用以下方式自定义按钮的内边距:
swipeableView.buttonPadding = 10.0 //Default is 8.0
(带定制)
Sahil Mahajan,[email protected]
SMSwipeableTabView在MIT许可证下可用。有关更多信息,请参阅LICENSE文件。