简介
这篇文章主要介绍如何使用 YDRootNavigationController 实现以下功能,关于它的详细介绍可以查看这篇文章 Swift 全局默认导航栏样式与视图控制器自定义并存。
- 隐藏返回按钮
- 定制返回按钮
- 自定义返回按钮
- 自定义返回按钮点击事件
一、隐藏返回按钮
- 全局默认设置
- 视图控制器默认设置
全局默认设置
1.创建一个类用来实现YDAppAppearanceProtocol
协议
class MyAppAppearance: YDAppAppearanceProtocol {
// 如果不实现该计算属性,默认是显示
var isHidesBackItem: Bool { true }
}
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
设置
视图控制器默认设置
class ViewController: UIViewController {
override var isHidesBackItem: Bool { true }
}
二、定制返回按钮
- 全局默认设置
- 视图控制器默认设置
全局默认设置
1.创建一个类用来实现YDAppAppearanceProtocol
协议
class MyAppAppearance: YDAppAppearanceProtocol {
var backItemImage: UIImage? { UIImage(named: "nav_back_black_button") }
var backItemImageInsets: UIEdgeInsets? { UIEdgeInsets(top: 0, left: -6, bottom: 0, right: 0) }
}
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
设置
视图控制器默认设置
class ViewController: UIViewController {
// title只显示标题
override var backItemType: YDBackItemType { .title("Back") }
// title只显示图标
override var backItemType: YDBackItemType { .image(UIImage(named: "nav_circleback_button"), imageInsets: UIEdgeInsets(top: 0, left: -6, bottom: 0, right: 0)) }
// title图片加标题
override var backItemType: YDBackItemType { .all("title", titleTextAttributes: [NSAttributedString.Key.foregroundColor : UIColor.randomColor], image: UIImage(named: "nav_back_black_button"), contentInsets: UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)) }
}
注意:default
样式是全局默认配置,如果全局默认返回按钮样式未配置,则显示原生的返回按钮,这是自定返回按钮点击事件就会失效。
三、自定义返回按钮
class ViewController: UIViewController {
override var backItem: UIBarButtonItem? { UIBarButtonItem(title: "BackItem", style: .plain, target: self, action: #selector(backItemAction)) }
override func backItemAction(_ sender: Any?) {
let alert = UIAlertController(title: "提示", message: "是否确定退出?", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "确定", style: .default, handler: { [weak self] action in
self?.navigationController?.popViewController(animated: true)
}))
alert.addAction(UIAlertAction(title: "取消", style: .default))
present(alert, animated: true)
}
}
四、自定义返回按钮点击事件
class ViewController: UIViewController {
override func backItemAction(_ sender: Any?) {
let alert = UIAlertController(title: "提示", message: "是否确定退出?", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "确定", style: .default, handler: { [weak self] action in
self?.navigationController?.popViewController(animated: true)
}))
alert.addAction(UIAlertAction(title: "取消", style: .default))
present(alert, animated: true)
}
}