MoviaBox/ShortPlay/Base/Controller/SPViewController.swift
2025-04-14 16:32:48 +08:00

169 lines
4.9 KiB
Swift

//
// 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)
}
}
}