| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- //
- // LNViewController.swift
- // Lanu
- //
- // Created by OneeChan on 2025/11/6.
- //
- import Foundation
- import UIKit
- class LNViewController: UIViewController {
- var showNavigationBar = true
- var enableDragBack = true
- var navigationBarColor: UIColor = .white
- var customBack: (() -> Void)?
-
- override func viewDidLoad() {
- super.viewDidLoad()
-
- view.backgroundColor = .white
- edgesForExtendedLayout = []
- // hidesBottomBarWhenPushed = true
-
- let button = UIButton(type: .system)
- let buttonImage: UIImage? = .init(systemName: "chevron.backward")?.withRenderingMode(.alwaysTemplate)
- button.contentHorizontalAlignment = .center
- button.setImage(buttonImage, for: .normal)
- button.tintColor = .black
- button.addAction(UIAction(handler: { [weak self] _ in
- guard let self else { return }
- if let customBack {
- customBack()
- } else {
- navigationController?.popViewController(animated: true)
- }
- }), for: .touchUpInside)
- navigationItem.leftBarButtonItem = UIBarButtonItem(customView: button)
- }
-
- func setRightButton(_ view: UIView?) {
- if let view {
- navigationItem.rightBarButtonItem = UIBarButtonItem(customView: view)
- } else {
- navigationItem.rightBarButtonItem = nil
- }
- }
-
- override func viewWillAppear(_ animated: Bool) {
- super.viewWillAppear(animated)
-
- navigationController?.setNavigationBarHidden(!showNavigationBar, animated: animated)
-
- if showNavigationBar,
- let navBar = navigationController?.navigationBar {
- // 1. 配置外观(iOS 15+ 必须用 UINavigationBarAppearance)
- let appearance = UINavigationBarAppearance()
- appearance.backgroundColor = navigationBarColor // 导航栏背景色
- appearance.shadowColor = .clear // 去除底部阴影线
- if navigationBarColor == .clear {
- appearance.configureWithTransparentBackground()
- }
-
- // 2. 应用外观设置(iOS 15+ 需要设置 scrollEdgeAppearance 和 standardAppearance)
- navBar.scrollEdgeAppearance = appearance // 滚动到顶部时的外观(如列表顶部)
- navBar.standardAppearance = appearance // 常规状态的外观
- navBar.compactAppearance = appearance
- }
- }
- }
|