位于标签栏上的固定和可折叠的视图控制器
需求
- iOS 10.0
- 只要在顶部分配了一个粘性视图控制器(任何在任何地方推送的vc都不应将
hidesBottomBarWhenPushed
设置为true
),则标签栏即可显示。
安装
StickyTabBarViewController 通过 CocoaPods 提供使用。要安装它,只需在 Podfile 中添加以下行
pod 'StickyTabBarViewController', '1.0.5'
使用
从您的标签控制器中派生自StickyViewControllerSupportingTabBarController
。
可以直接从标签控制器配置动画持续时间或折叠视图高度。
从viewDidLoad
方法开始。
import UIKit
import StickyTabBarViewController
class MainTabBarController: StickyViewControllerSupportingTabBarController {
override func viewDidLoad() {
super.viewDidLoad()
collapsedHeight = 50.0
animationDuration = 0.5
}
}
通过覆盖标签控制器的初始化器。
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
// here if you are using xib
collapsedHeight = 50.0
animationDuration = 0.5
}
required init?(coder: NSCoder) {
super.init(coder: coder)
// configure also on required init (if you are using storyboard for example)
collapsedHeight = 50.0
animationDuration = 0.5
}
也可以通过访问tabBarController在任何时候更新它。
显示的视图控制器配置
任何需要粘性行为的视图控制器必须遵循 Expandable
并实现一个 minimisedView
。
实现 minimisedView
应该被理想地锚定在视图控制器视图的顶部,并且其高度(无论是通过直接的高度约束还是其他约束)应等于 collapsedHeight
的值。您不需要担心隐藏或显示它,因为由 StickyTabBarViewController 本身处理。
var minimisedView: UIView {
return UIView() // or return your outlet for minimised view.
}
按照以下方式从遵循 Expandable
的视图控制器折叠粘性视图
container?.collapseChild()
按照以下方式从遵循 Expandable
的视图控制器展开粘性视图
container?.expandChild()
按照以下方式从遵循 Expandable
的视图控制器移除粘性视图
container?.removeCollapsableChild(animated:)
按照以下方式配置粘性子视图控制器
if let tabBarController = tabBarController as? StickyViewControllerSupportingTabBarController {
let viewControllerToStick = SampleChildViewController()
tabBarController.configureCollapsableChild(viewControllerToStick,
isFullScreenOnFirstAppearance: true)
}
与展示的粘性子视图控制器从任何地方通过tabBarController访问交互
通过tabBarController访问来与粘性子视图控制器交互
var tabController: StickyViewControllerSupportingTabBarController? {
if let tabBarController = tabBarController as? StickyViewControllerSupportingTabBarController {
return tabBarController
}
return nil
}
展开/折叠子视图控制器
tabController?.collapseChild()
tabController?.expandChild()
待改进功能
- 在展开时能够以参数化的方式隐藏标签栏和状态栏,这将会很棒。
- 目前无法配置或覆盖已实现的粘性视图控制器,您必须先移除它,然后如果需要再实现另一个。也许如果在已经分配了视图控制器后调用配置函数时实现覆盖功能会比较好?