125 lines
4.8 KiB
Swift
125 lines
4.8 KiB
Swift
//
|
|
// BaseNavigationController.swift
|
|
// SHECommunity
|
|
//
|
|
// Created by 林 on 2024/11/25.
|
|
//
|
|
|
|
import UIKit
|
|
|
|
class BaseNavigationController: UINavigationController {
|
|
|
|
var isPresented: Bool {
|
|
return viewControllers.count == 1 && presentingViewController != nil
|
|
}
|
|
|
|
@objc func dismissClicked(_ item: UIBarButtonItem) {
|
|
self.navigationController?.dismiss(animated: true, completion: nil)
|
|
}
|
|
|
|
@objc func leftBackClicked(_ item: UIBarButtonItem) {
|
|
self.navigationController?.popViewController(animated: true)
|
|
}
|
|
|
|
func setupLeftItem() {
|
|
guard !isNavigationBarHidden else { return }
|
|
if isPresented {
|
|
let dismissItem = UIBarButtonItem(image: UIImage(named: "Common/close"),
|
|
style: .plain,
|
|
target: self,
|
|
action: #selector(dismissClicked(_:)))
|
|
navigationItem.leftBarButtonItem = dismissItem
|
|
} else if let count = navigationController?.viewControllers.count, count > 1 {
|
|
let leftBackItem = UIBarButtonItem(image: UIImage(named: "Common/back_white"),
|
|
style: .plain,
|
|
target: self, action: #selector(leftBackClicked(_:)))
|
|
navigationItem.leftBarButtonItem = leftBackItem
|
|
}
|
|
}
|
|
|
|
func setNavibar() {
|
|
// self.navigationBar.barTintColor = Color.mainColor
|
|
// self.navigationBar.tintColor = .white
|
|
// self.navigationBar.shadowColor = Color.rgbCCCCCC
|
|
self.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white,
|
|
NSAttributedString.Key.font:UIFont.systemFont(ofSize: 18, weight: .semibold)];
|
|
// self.navigationItem.backBarButtonItem = UIBarButtonItem.init(title: "", style: .plain, target: nil, action: nil);
|
|
}
|
|
|
|
override var preferredStatusBarStyle: UIStatusBarStyle {
|
|
if let vc = self.viewControllers.last {
|
|
return vc.preferredStatusBarStyle
|
|
} else {
|
|
return .lightContent
|
|
}
|
|
}
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
setNavigationBarHidden(true, animated: false)
|
|
// if #available(iOS 13, *) {
|
|
// let appearance = UINavigationBarAppearance()
|
|
// appearance.configureWithOpaqueBackground()
|
|
//// appearance.backgroundImage = UIImage(named: "navigation-back-img")
|
|
// appearance.shadowImage = UIImage()
|
|
// appearance.shadowColor = Color.rgbCCCCCC
|
|
// appearance.backgroundColor = Color.mainColor
|
|
// appearance.titleTextAttributes = [NSAttributedString.Key.font:UIFont.boldSystemFont(ofSize: 18),
|
|
// NSAttributedString.Key.foregroundColor:UIColor.white]
|
|
// navigationBar.standardAppearance = appearance
|
|
// navigationBar.scrollEdgeAppearance = navigationBar.standardAppearance
|
|
// }
|
|
// else {
|
|
// navigationBar.barTintColor = Color.mainColor
|
|
// navigationBar.tintColor = .white
|
|
// navigationBar.shadowColor = Color.rgbCCCCCC
|
|
// navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white,
|
|
// NSAttributedString.Key.font:UIFont.boldSystemFont(ofSize:18)]
|
|
//
|
|
// }
|
|
|
|
}
|
|
override func viewWillAppear(_ animated: Bool) {
|
|
super.viewWillAppear(animated)
|
|
setNavigationBarHidden(true, animated: false)
|
|
}
|
|
|
|
override func pushViewController(_ viewController: UIViewController, animated: Bool) {
|
|
|
|
if self.viewControllers.count == 0 {
|
|
super.pushViewController(viewController, animated: animated)
|
|
return
|
|
}
|
|
// if let vc = viewController as? BaseViewController, vc.requireLogin() && !isLogin() {
|
|
// toLogin()
|
|
// return
|
|
// }
|
|
// self.setNavigationBarHidden(true, animated: false)
|
|
viewController.hidesBottomBarWhenPushed = true
|
|
super.pushViewController(viewController, animated: animated)
|
|
}
|
|
|
|
override var shouldAutorotate: Bool {
|
|
if let vc = self.viewControllers.last {
|
|
return vc.shouldAutorotate
|
|
} else {
|
|
return false
|
|
}
|
|
}
|
|
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
|
|
if let vc = self.viewControllers.last {
|
|
return vc.supportedInterfaceOrientations
|
|
} else {
|
|
return .portrait
|
|
}
|
|
}
|
|
override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
|
|
if let vc = self.viewControllers.last {
|
|
return vc.preferredInterfaceOrientationForPresentation
|
|
} else {
|
|
return .portrait
|
|
}
|
|
}
|
|
|
|
}
|