在标签栏顶部的粘性且可折叠的视图控制器
hidesBottomBarWhenPushed
设置为true
)。StickyTabBarViewController 可通过 SPM 和 CocoaPods 获取。
SPM 安装:将其作为新的包依赖项添加,地址为 https://github.com/emrepun/StickyTabBarViewController.git
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 以与粘性子视图控制器交互
var tabController: StickyViewControllerSupportingTabBarController? {
if let tabBarController = tabBarController as? StickyViewControllerSupportingTabBarController {
return tabBarController
}
return nil
}
展开/折叠子视图控制器
tabController?.collapseChild()
tabController?.expandChild()