LNViewController.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //
  2. // LNViewController.swift
  3. // Lanu
  4. //
  5. // Created by OneeChan on 2025/11/6.
  6. //
  7. import Foundation
  8. import UIKit
  9. import SnapKit
  10. import Combine
  11. class LNViewController: UIViewController {
  12. let backButton = UIButton()
  13. var showNavigationBar = true
  14. var enableDragBack = true
  15. var isIdleTimerDisabled = false
  16. var navigationBarColor: UIColor = .white {
  17. didSet {
  18. guard let navBar = navigationController?.navigationBar else { return }
  19. navBar.standardAppearance.backgroundColor = navigationBarColor
  20. navBar.scrollEdgeAppearance?.backgroundColor = navigationBarColor
  21. navBar.compactAppearance?.backgroundColor = navigationBarColor
  22. }
  23. }
  24. var customBack: (() -> Void)?
  25. let fakeNaviBgView = UIView()
  26. override func viewDidLoad() {
  27. super.viewDidLoad()
  28. view.backgroundColor = .white
  29. edgesForExtendedLayout = []
  30. // hidesBottomBarWhenPushed = true
  31. let buttonImage: UIImage? = .init(systemName: "chevron.backward")?.withRenderingMode(.alwaysTemplate)
  32. backButton.contentHorizontalAlignment = .center
  33. backButton.setImage(buttonImage, for: .normal)
  34. backButton.tintColor = .black
  35. backButton.addAction(UIAction(handler: { [weak self] _ in
  36. guard let self else { return }
  37. if let customBack {
  38. customBack()
  39. } else {
  40. navigationController?.popViewController(animated: true)
  41. }
  42. }), for: .touchUpInside)
  43. backButton.snp.makeConstraints { make in
  44. make.width.height.equalTo(24)
  45. }
  46. navigationItem.leftBarButtonItem = UIBarButtonItem(customView: backButton)
  47. view.addSubview(fakeNaviBgView)
  48. fakeNaviBgView.snp.makeConstraints { make in
  49. make.horizontalEdges.equalToSuperview()
  50. make.bottom.equalTo(view.snp.top)
  51. make.height.equalTo(UIView.navigationBarHeight + UIView.statusBarHeight)
  52. }
  53. view.publisher(for: \.frame).removeDuplicates().sink
  54. { [weak self] newValue in
  55. guard let self else { return }
  56. if newValue.origin.y > fakeNaviBgView.bounds.height {
  57. fakeNaviBgView.snp.updateConstraints { make in
  58. make.height.equalTo(newValue.origin.y)
  59. }
  60. }
  61. }.store(in: &cancellables)
  62. }
  63. func setRightButton(_ view: UIView?) {
  64. if let view {
  65. navigationItem.rightBarButtonItem = UIBarButtonItem(customView: view)
  66. } else {
  67. navigationItem.rightBarButtonItem = nil
  68. }
  69. }
  70. func setTitleView(_ contentView: UIView) {
  71. navigationItem.titleView = contentView
  72. }
  73. override func viewWillAppear(_ animated: Bool) {
  74. super.viewWillAppear(animated)
  75. navigationController?.setNavigationBarHidden(!showNavigationBar, animated: animated)
  76. if showNavigationBar,
  77. let navBar = navigationController?.navigationBar {
  78. navBar.backgroundColor = .clear
  79. let appearance = UINavigationBarAppearance()
  80. appearance.configureWithTransparentBackground()
  81. appearance.backgroundColor = navigationBarColor // 导航栏背景色
  82. appearance.shadowColor = .clear // 去除底部阴影线
  83. navBar.scrollEdgeAppearance = appearance // 滚动到顶部时的外观(如列表顶部)
  84. navBar.standardAppearance = appearance // 常规状态的外观
  85. navBar.compactAppearance = appearance
  86. }
  87. fakeNaviBgView.backgroundColor = view.backgroundColor
  88. UIApplication.shared.isIdleTimerDisabled = isIdleTimerDisabled
  89. }
  90. }