// // UINavigationBar+SPAdd.swift // ShortPlay // // Created by 曾觉新 on 2025/4/8. // import UIKit extension UINavigationBar { static let sp_normalTitleFont = UIFont.fontBold(ofSize: 15) static var sp_normalTitleColor: UIColor { get { return .colorFFFFFF() } } /** 默认标题样式 */ static var sp_normalTitleTextAttributes: [NSAttributedString.Key : Any] { get { return [ NSAttributedString.Key.font : sp_normalTitleFont, NSAttributedString.Key.foregroundColor : sp_normalTitleColor ] } } /** 默认背景色 */ static var sp_normalBackgroundColor: UIColor { get { return .themeColor() } } @available(iOS 13.0, *) static let navBarAppearance: UINavigationBarAppearance = { let navBarAppearance = UINavigationBarAppearance() navBarAppearance.configureWithOpaqueBackground() // 背景色 navBarAppearance.backgroundColor = sp_normalBackgroundColor // 去掉半透明效果 navBarAppearance.backgroundEffect = nil // 去除导航栏阴影(如果不设置clear,导航栏底下会有一条阴影线) navBarAppearance.shadowColor = UIColor.clear // 字体颜色 navBarAppearance.titleTextAttributes = sp_normalTitleTextAttributes return navBarAppearance }() func sp_setTranslucent(isTranslucent: Bool) { self.isTranslucent = isTranslucent } func sp_setBackgroundColor(backgroundColor: UIColor?) { if #available(iOS 15.0, *) { UINavigationBar.navBarAppearance.backgroundColor = backgroundColor self.standardAppearance = UINavigationBar.navBarAppearance self.scrollEdgeAppearance = UINavigationBar.navBarAppearance } if let backgroundColor = backgroundColor { self.setBackgroundImage(UIImage(color: backgroundColor), for: .default) // self.barTintColor = backgroundColor } else { self.setBackgroundImage(UIImage(), for: .default) // self.barTintColor = nil } } func sp_setTitleTextAttributes(titleTextAttributes: [NSAttributedString.Key : Any]?) { if #available(iOS 15.0, *) { if let titleTextAttributes = titleTextAttributes { UINavigationBar.navBarAppearance.titleTextAttributes = titleTextAttributes } self.scrollEdgeAppearance = UINavigationBar.navBarAppearance self.standardAppearance = UINavigationBar.navBarAppearance } else { self.titleTextAttributes = titleTextAttributes } } }