简介
这篇文章主要介绍如何使用 YDRootNavigationController 实现返回手势和全屏返回手势的功能,关于它的详细介绍可以查看这篇文章 Swift 全局默认导航栏样式与视图控制器自定义并存。
UIScrollView与返回手势.gif
地图与返回手势.gif
一、全屏返回手势
- 全局默认设置
- 视图控制器默认设置
- 视图控制器中动态设置
全局默认设置
1.创建一个类用来实现YDAppAppearanceProtocol
协议
class MyAppAppearance: YDAppAppearanceProtocol {
// 如果不实现该计算属性,默认是开启
var isInteractivePopGestureEnabled: Bool { false }
}
2.在AppDelegate
中调用
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// 全局默认样式配置
MyAppAppearance().configure()
return true
}
}
3.导航栏控制器设置为YDRootNavigationController
或继承YDRootNavigationController
的类
- 代码
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
self.window?.rootViewController = YDRootNavigationController()
return true
}
-
Interface Builder
设置
IB设置YDRootNavigationController.gif
视图控制器默认设置
class ViewController: UIViewController {
// 默认设置
override var isInteractivePopGestureEnabled: Bool { false }
}
视图控制器中动态设置
class ViewController: UIViewController {
// 默认设置
override var isInteractivePopGestureEnabled: Bool { interactivePopGesture }
var interactivePopGesture: Bool = true {
didSet {
// 动态设置
interactivePopGesture(interactivePopGesture)
}
}
}
返回手势.gif
二、全屏返回手势
- 全局默认设置
- 视图控制器默认设置
- 视图控制器中动态设置
全局默认设置
同返回手势设置方法一致,对应属性名称改为isFullScreenPopGestureEnabled
即可
视图控制器默认设置
class ViewController: UIViewController {
// 默认设置
override var isFullScreenPopGestureEnabled: Bool { false }
}
视图控制器中动态设置
class ViewController: UIViewController {
// 默认设置
override var isFullScreenPopGestureEnabled: Bool { fullScreenPopGesture }
var fullScreenPopGesture: Bool = true {
didSet {
// 动态设置
fullScreenPopGesture(fullScreenPopGesture)
}
}
}
全屏返回手势.gif