美文网首页
Swift 隐藏返回按钮和自定义返回按钮

Swift 隐藏返回按钮和自定义返回按钮

作者: jsone | 来源:发表于2024-03-28 12:46 被阅读0次

简介

这篇文章主要介绍如何使用 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设置
    IB设置YDRootNavigationController.gif

视图控制器默认设置

class ViewController: UIViewController {
    override var isHidesBackItem: Bool { true }
}
返回按钮隐藏.gif

二、定制返回按钮

  • 全局默认设置
  • 视图控制器默认设置

全局默认设置

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设置
    IB设置YDRootNavigationController.gif

视图控制器默认设置

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样式是全局默认配置,如果全局默认返回按钮样式未配置,则显示原生的返回按钮,这是自定返回按钮点击事件就会失效。

返回按钮样式.gif

三、自定义返回按钮

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)
    }
}
自定义返回按钮.gif

四、自定义返回按钮点击事件

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)
    }
}
返回按钮点击事件.gif

相关文章

网友评论

      本文标题:Swift 隐藏返回按钮和自定义返回按钮

      本文链接:https://www.haomeiwen.com/subject/geeutjtx.html