LNViewController.swift 3.6 KB

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