197 lines
6.1 KiB
Swift
197 lines
6.1 KiB
Swift
//
|
|
// SPViewController.swift
|
|
// Thimra
|
|
//
|
|
// 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;
|
|
}()
|
|
|
|
///头部渐变色
|
|
private lazy var topGradientView: SPGradientView = {
|
|
let view = SPGradientView()
|
|
view.colors = [UIColor.color290D0F().cgColor, UIColor.color230E11().cgColor, UIColor.color181115().cgColor, UIColor.color121317(alpha: 0).cgColor]
|
|
view.startPoint = .init(x: 0.5, y: 0)
|
|
view.endPoint = .init(x: 0.5, y: 1)
|
|
view.locations = [0, 0.33, 0.66, 1]
|
|
return view
|
|
}()
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
self.isViewDidLoad = true
|
|
self.edgesForExtendedLayout = []
|
|
|
|
view.addSubview(topGradientView)
|
|
view.addSubview(bgImageView)
|
|
|
|
topGradientView.snp.makeConstraints { make in
|
|
make.left.right.top.equalToSuperview()
|
|
make.height.equalTo(kSPStatusbarHeight + 420)
|
|
}
|
|
|
|
bgImageView.snp.makeConstraints { make in
|
|
make.left.right.top.equalToSuperview()
|
|
}
|
|
|
|
setBackgroundView()
|
|
|
|
if let navi = navigationController {
|
|
if navi.visibleViewController == self {
|
|
if navi.viewControllers.count > 1 {
|
|
configNavigationBack()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func setBackgroundView(isShowGradient: Bool = true, bgImage: UIImage? = UIImage(named: "main_bg_image_01"), backgroundColor: UIColor = UIColor.backgroundColor()) {
|
|
topGradientView.isHidden = !isShowGradient
|
|
bgImageView.isHidden = isShowGradient
|
|
bgImageView.image = bgImage
|
|
view.backgroundColor = backgroundColor
|
|
}
|
|
|
|
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 .lightContent
|
|
}
|
|
}
|
|
override var prefersStatusBarHidden: Bool {
|
|
return statusBarHidden
|
|
}
|
|
///是否支持自动旋屏
|
|
override var shouldAutorotate: Bool {
|
|
return false
|
|
}
|
|
///屏幕方向
|
|
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
|
|
return .portrait
|
|
}
|
|
|
|
///子类实现
|
|
func fetchChildControllerScrollView() -> UIScrollView? {
|
|
return nil
|
|
}
|
|
|
|
func handleHeaderRefresh(_ completer: (() -> Void)?) {}
|
|
|
|
func handleFooterRefresh(_ completer: (() -> Void)?) {}
|
|
|
|
}
|
|
|
|
extension UIViewController {
|
|
|
|
func configNavigationBack(_ imageName: String = "arrow_left_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)
|
|
}
|
|
}
|
|
}
|