// // LNViewController.swift // Lanu // // Created by OneeChan on 2025/11/6. // import Foundation import UIKit import SnapKit import Combine class LNViewController: UIViewController { let backButton = UIButton() var showNavigationBar = true var enableDragBack = true var isIdleTimerDisabled = false var navigationBarColor: UIColor = .white { didSet { guard let navBar = navigationController?.navigationBar else { return } navBar.standardAppearance.backgroundColor = navigationBarColor navBar.scrollEdgeAppearance?.backgroundColor = navigationBarColor navBar.compactAppearance?.backgroundColor = navigationBarColor } } var customBack: (() -> Void)? let fakeNaviBgView = UIView() override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = .white edgesForExtendedLayout = [] // hidesBottomBarWhenPushed = true let buttonImage: UIImage? = .init(systemName: "chevron.backward")?.withRenderingMode(.alwaysTemplate) backButton.contentHorizontalAlignment = .center backButton.setImage(buttonImage, for: .normal) backButton.tintColor = .black backButton.addAction(UIAction(handler: { [weak self] _ in guard let self else { return } if let customBack { customBack() } else { navigationController?.popViewController(animated: true) } }), for: .touchUpInside) backButton.snp.makeConstraints { make in make.width.height.equalTo(24) } navigationItem.leftBarButtonItem = UIBarButtonItem(customView: backButton) view.addSubview(fakeNaviBgView) fakeNaviBgView.snp.makeConstraints { make in make.horizontalEdges.equalToSuperview() make.bottom.equalTo(view.snp.top) make.height.equalTo(UIView.navigationBarHeight + UIView.statusBarHeight) } view.publisher(for: \.frame).removeDuplicates().sink { [weak self] newValue in guard let self else { return } if newValue.origin.y > fakeNaviBgView.bounds.height { fakeNaviBgView.snp.updateConstraints { make in make.height.equalTo(newValue.origin.y) } } }.store(in: &cancellables) } func setRightButton(_ view: UIView?) { if let view { navigationItem.rightBarButtonItem = UIBarButtonItem(customView: view) } else { navigationItem.rightBarButtonItem = nil } } func setTitleView(_ contentView: UIView) { navigationItem.titleView = contentView } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) navigationController?.setNavigationBarHidden(!showNavigationBar, animated: animated) if showNavigationBar, let navBar = navigationController?.navigationBar { navBar.backgroundColor = .clear let appearance = UINavigationBarAppearance() appearance.configureWithTransparentBackground() appearance.backgroundColor = navigationBarColor // 导航栏背景色 appearance.shadowColor = .clear // 去除底部阴影线 navBar.scrollEdgeAppearance = appearance // 滚动到顶部时的外观(如列表顶部) navBar.standardAppearance = appearance // 常规状态的外观 navBar.compactAppearance = appearance } fakeNaviBgView.backgroundColor = view.backgroundColor UIApplication.shared.isIdleTimerDisabled = isIdleTimerDisabled } }