123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- //
- // IHNavigationController.swift
- // Inhealth
- //
- // Created by weclouds on 2019/12/10.
- // Copyright © 2019 weclouds. All rights reserved.
- //
- import UIKit
- ///导航栏返回协议
- @objc protocol NavigationProtocol{
- /// 导航栏将要返回的方法
- ///
- /// - Returns : true: 返回上一界面 ,false: 禁止返回
- @objc optional func navigationShouldPopMethod() -> Bool
- }
- class IHNavigationController: UINavigationController {
- override func viewDidLoad() {
- super.viewDidLoad()
- setNavigationBar()
- interactivePopGestureRecognizer?.delegate = self
- }
-
- func setNavigationBar() {
-
- let apperance = UINavigationBar.appearance()
- //设置半透明效果
- self.navigationBar.isTranslucent = false
-
- //导航栏上按钮颜色
- apperance.tintColor = UIColor.black
- //设置导航栏颜色
- apperance.barTintColor = UIColor(hexString: "#FFFFFF")
- //去除分割线
- // apperance.setBackgroundImage(UIImage(), for: .default)
- apperance.shadowImage = UIImage()
- let dict = [NSAttributedString.Key.foregroundColor:UIColor.white,NSAttributedString.Key.font:UIFont(name: PingFangSC_Semibold, size: 18) ]
- apperance.titleTextAttributes = dict as Any as? [NSAttributedString.Key : Any]
- let backAppearance = UIBarButtonItem.appearance()
- //设置导航栏返回按钮
- self.navigationBar.topItem?.title = ""
- if #available(iOS 11, *){
- // backAppearance.setBackButtonTitlePositionAdjustment(UIOffset(horizontal: -200, vertical: 0), for: UIBarMetrics.default)
- //
- // let backButtonImage = UIImage(named: "返回")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
- //
- // apperance.backIndicatorImage = backButtonImage
- // apperance.backIndicatorTransitionMaskImage = backButtonImage
-
- let backButtonImage = UIImage(named: "返回")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
-
- apperance.backIndicatorImage = backButtonImage
- apperance.backIndicatorTransitionMaskImage = backButtonImage
-
- let leftBtn = UIBarButtonItem.init(image: backButtonImage, style: .plain, target: self, action: #selector(clickBack))
- self.navigationItem.leftBarButtonItem = leftBtn
-
- }else {
- //返回按钮没有文字
- backAppearance.setBackButtonTitlePositionAdjustment(UIOffset(horizontal: -200, vertical: 0), for: UIBarMetrics.default)
- let backButtonImage = UIImage(named: "返回")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
- backAppearance.setBackButtonBackgroundImage(backButtonImage?.resizableImage(withCapInsets: UIEdgeInsets(top: 0, left: 32, bottom: 0, right: 0)), for: UIControl.State.normal, barMetrics: UIBarMetrics.default)
-
- }
- }
-
- override func pushViewController(_ viewController: UIViewController, animated: Bool) {
- if self.viewControllers.count > 0 {
- viewController.hidesBottomBarWhenPushed = true
- viewController.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: UIBarButtonItem.Style.plain, target: self, action: #selector(IHNavigationController.backAction ))
- }
- super.pushViewController(viewController, animated: animated)
- }
-
- @objc func backAction() {
-
- self.navigationController?.popViewController(animated: true)
- }
-
- @objc private func clickBack(){
-
- }
-
- }
- extension UIViewController :NavigationProtocol{
- func navigationShouldPopMethod() -> Bool {
- return true
- }
- }
- extension UINavigationController:UINavigationBarDelegate,UIGestureRecognizerDelegate{
- public func navigationBar(_ navigationBar: UINavigationBar, shouldPop item: UINavigationItem) -> Bool {
- if viewControllers.count < navigationBar.items!.count {
- return true
- }
- var shouldPop = false
- let vc: UIViewController = topViewController!
- if vc.responds(to: #selector(navigationShouldPopMethod)) {
- shouldPop = vc.navigationShouldPopMethod()
- }
-
- if shouldPop {
- DispatchQueue.main.async {
- self.popViewController(animated: true)
- }
- }else{
- for subview in navigationBar.subviews {
- if 0.0 < subview.alpha && subview.alpha < 1.0 {
- UIView.animate(withDuration: 0.25) {
- subview.alpha = 1.0
- }
- }
- }
- }
- return false
- }
-
- public func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
- if children.count == 1 {
- return false
- }else{
- if topViewController?.responds(to: #selector(navigationShouldPopMethod)) != nil {
- return topViewController!.navigationShouldPopMethod()
- }
- return true
- }
- }
-
- open override var preferredStatusBarStyle: UIStatusBarStyle{
-
- return self.topViewController!.preferredStatusBarStyle
- }
-
- open override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation{
- return .none
- }
- open override var prefersStatusBarHidden: Bool{
- return false
- }
-
-
- }
|