// // SPViewController.swift // ShortPlay // // Created by 曾觉新 on 2025/4/8. // import UIKit class SPViewController: UIViewController, JYPageChildContollerProtocol { var statusBarStyle: UIStatusBarStyle? { didSet { self.setNeedsStatusBarAppearanceUpdate() } } var statusBarHidden: Bool = false { didSet { // self.setNeedsStatusBarAppearanceUpdate() } } private(set) var isViewDidLoad = false private(set) var isDidAppear = false private(set) lazy var _bgImageView: UIImageView = { let imageView = UIImageView(image: UIImage(named: "main_bg_image_01")) imageView.isUserInteractionEnabled = true return imageView; }() override func viewDidLoad() { super.viewDidLoad() self.isViewDidLoad = true self.edgesForExtendedLayout = [] setBgImageView() if let navi = navigationController { if navi.visibleViewController == self { if navi.viewControllers.count > 1 { configNavigationBack() } } } } func setBgImageView() { self.view = self._bgImageView self.view.backgroundColor = .black } override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) isDidAppear = true } override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) isDidAppear = false } override func viewDidDisappear(_ animated: Bool) { super.viewDidDisappear(animated) isDidAppear = false } //MARK:-------------- 状态栏样式 -------------- override var preferredStatusBarStyle: UIStatusBarStyle { if let statusBarStyle = statusBarStyle { return statusBarStyle } else { return .default } } override var prefersStatusBarHidden: Bool { return statusBarHidden } ///是否支持自动旋屏 override var shouldAutorotate: Bool { return false } ///屏幕方向 override var supportedInterfaceOrientations: UIInterfaceOrientationMask { return .portrait } ///子类实现 func fetchChildControllerScrollView() -> UIScrollView? { return nil } } extension UIViewController { func configNavigationBack(_ imageName: String = "left_arrow_icon_01") { let image = UIImage(named: imageName) let leftBarButtonItem = UIBarButtonItem(image: image, style: .plain ,target: self,action: #selector(handleBack)) navigationItem.leftBarButtonItem = leftBarButtonItem } @objc func handleBack() { self.sp_toLastViewController(animated: true) } } extension UIViewController { func sp_toLastViewController(animated:Bool) { if self.navigationController != nil { if self.navigationController?.viewControllers.count == 1 { self.dismiss(animated: animated, completion: nil) } else { self.navigationController?.popViewController(animated: animated) } } else if self.presentingViewController != nil { self.dismiss(animated: animated, completion: nil) } } } extension UIViewController { ///设置导航默认样式 func setNavigationNormalStyle(backgroundColor: UIColor = UINavigationBar.sp_normalBackgroundColor, isTranslucent: Bool = false, prefersLargeTitles: Bool = false) { self.setNavigationBackgroundColor(color: backgroundColor, isTranslucent: isTranslucent) self.setNavigationTitleStyle() self.navigationController?.navigationBar.prefersLargeTitles = prefersLargeTitles } ///设置导航背景色 func setNavigationBackgroundColor(color: UIColor?, isTranslucent: Bool = false) { guard let nav = navigationController else { return } if nav.visibleViewController == self { nav.navigationBar.sp_setBackgroundColor(backgroundColor: color) nav.navigationBar.sp_setTranslucent(isTranslucent: isTranslucent) } } ///设置当行标题样式 func setNavigationTitleStyle(color: UIColor? = nil, font: UIFont? = nil) { guard let nav = navigationController else { return } if nav.visibleViewController == self { //标题样式 var titleTextAttributes = UINavigationBar.sp_normalTitleTextAttributes if let color = color { titleTextAttributes[NSAttributedString.Key.foregroundColor] = color } if let font = font { titleTextAttributes[NSAttributedString.Key.font] = font } nav.navigationBar.sp_setTitleTextAttributes(titleTextAttributes: titleTextAttributes) } } }