WXNavigationBar
微信NavigationBar
功能
- 支持透明导航栏
- 支持导航栏背景图片
- 支持导航栏大标题模式
- 支持iOS 13暗黑模式
- 支持全屏返回手势
- 使用起来和UINavigationBar一样简单
要求
- iOS 9.0+
- Xcode 11.0+
- Swift 5.0+
安装
CocoaPods
WXNavigationBar
可以通过CocoaPods获得。要安装它,只需在Podfile中添加以下行。
use_frameworks!
pod 'WXNavigationBar', '~> 2.3.6'
Carthage
Carthage是一种去中心化的依赖关系管理器,它构建您的依赖并为您提供二进制框架。要将WXNavigationBar集成到Xcode项目中使用Carthage,请在Cartfile中指定。
github alexiscn/WXNavigationBar
Swift Package Manager
Swift包管理器是一个用于自动化Swift代码分发的工具,它集成到swift编译器中。它仍处于早期开发阶段,但WXNavigationBar支持在支持平台上的使用。
一旦设置了Swift包,将WXNavigationBar作为依赖项添加就像将它添加到Package.swift的依赖项值一样简单。
dependencies: [
.package(url: "https://github.com/alexiscn/WXNavigationBar.git", .upToNextMajor(from: "2.3.6"))
]
设计原则
WXNavigation
使实际的UINavigationBar透明,并在视图控制器中添加视图作为假的导航栏。
实际导航栏仍然处理触摸事件,假导航栏执行显示任务,例如:backgroundColor, backgroundImage。
因此,您可以使用常规的导航栏。当您想要处理显示任务时,请使用WXNavigationBar
入门指南
在你的 AppDelegate.swift
import WXNavigationBar
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// ...
WXNavigationBar.setup()
}
如果需要,您可以自定义 WXNavigationBar
。配置 WXNavigationBar 有两种方式:通过 WXNavigationBar.NavBar
或通过 UIViewController
属性。
基于 UINavigationController 的配置
使用 WXNavigationBar.NavBar
来配置 WXNavigationBar
将影响所有视图控制器。
在你的 AppDelegate.swift
import WXNavigationBar
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// ...
// Customize WXNavigationBar if needed (Optional)
// WXNavigationBar.NavBar.backImage = UIImage(named: "xxx")
}
您可以配置以下选项
/// Back Image for Navigation Bar
public static var backImage: UIImage? = Utility.backImage
/// Use custom view to create back button.
/// Note: You do not need to add tap event to custom view. It's handled in WXNavigationBar.
public static var backButtonCustomView: UIView? = nil
/// Background Image for NavigationBar
public static var backgroundImage: UIImage? = nil
/// Background color for NavigationBar
public static var backgroundColor: UIColor = UIColor(white: 237.0/255, alpha: 1.0)
/// Tint Color for NavigationBar
public static var tintColor = UIColor(white: 24.0/255, alpha: 1.0)
/// Shadow Image for NavigationBar
public static var shadowImage: UIImage? = UIImage()
/// Enable fullscreen pop gesture
public static var fullscreenPopGestureEnabled = false
基于 ViewController 的配置
您也可以通过覆写 WXNavigationBar
支持的属性来配置特定的视图控制器。
背景颜色
您可以配置导航栏的背景颜色。
/// Background color of fake NavigationBar
/// Default color is UIColor(white: 237.0/255, alpha: 1.0)
override var wx_navigationBarBackgroundColor: UIColor? {
return .white
}
背景图片
您可以使用图片来配置导航栏的背景。
override var wx_navigationBarBackgroundImage: UIImage? {
return UIImage(named: "icons_navigation_bar")
}
系统模糊导航栏
如果您想使用类似系统的模糊导航栏
override var wx_useSystemBlurNavBar: Bool {
return true
}
NavigationBar的工具栏颜色调整
通过设置wx_barBarTintColor
,实际上是在设置navigationController?.navigationBar
的barTintColor
。
override var wx_barBarTintColor: UIColor? {
return .red
}
NavigationBar的颜色调整
通过设置wx_baTintColor
,实际上是在设置navigationController?.navigationBar
的tintColor
。
override var wx_barTintColor: UIColor? {
return .black
}
阴影图
可以为特定的视图控制器指定导航栏的阴影图像(例如:纯色线条或渐变颜色线条)。
override var wx_shadowImage: UIImage? {
return UIImage(named: "icons_navigation_bar_shadow_line")
}
阴影图颜色
可以从颜色创建阴影图像,这个属性将覆盖wx_shadowImage
。
override var wx_shadowImageTintColor: UIColor? {
return .red
}
返回按钮图
可以为特定的视图控制器指定导航栏的返回按钮图像。
override var wx_backImage: UIImage? {
return UIImage(named: "icons_view_controller_back_image")
}
自定义返回按钮视图
可以为返回按钮指定自定义视图。
override var wx_backButtonCustomView: UIView? {
let label = UILabel()
label.text = "back"
label.textAlignment = .center
return label
}
禁用交互式弹出手势
override var wx_disableInteractivePopGesture: Bool {
return true
}
全屏交互式弹出手势
override var wx_fullScreenInteractivePopEnabled: Bool {
return false
}
interactivePopMaxAllowedDistanceToLeftEdge
override wx_interactivePopMaxAllowedDistanceToLeftEdge: CGFloat {
return view.bounds.width * 0.5
}
高级使用
以下是针对 WXNavigationBar
的一些高级使用建议。
透明的导航栏
有几种方法可以实现导航栏透明。
alpha
wx_navigationBar.alpha = 0
hidden
wx_navigationBar.isHidden = true
背景颜色
override var wx_navigationBarBackgroundColor: UIColor? {
return .clear
}
alpha 和 hidden 使 wx_navigationBar 不可见,而 backgroundColor 只改变 wx_navigationBar 的颜色
动态更新导航栏
您可以在滚动时动态更新导航栏,例如。
有关详细信息,请参阅 MomentViewController
。
wx_navigationBar
wx_navigationBar
是 UIView 的子类,因此您可以像使用 UIView 一样使用 wx_navigationBar
。
处理返回按钮事件
如果您想在用户点击返回按钮时执行某些操作,您可以在您的视图中重写 wx_backButtonClicked
。例如,您可以在用户点击返回按钮时显示警告框。
override func wx_backButtonClicked() {
let alert = UIAlertController(title: "Are you sure to exit", message: nil, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { [weak self] _ in
self?.navigationController?.popViewController(animated: true)
}))
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { _ in
}))
present(alert, animated: true, completion: nil)
}
备注
子视图控制器
wx_navigationBar
在动态添加子视图控制器时可以进行覆盖。因此,您负责将 wx_navigationBar
拉到前台。
// addChild(controller) or addSubview
view.bringSubviewToFront(wx_navigationBar)
许可证
WXNavigationBar遵循MIT许可协议。 许可证
中文文档
您可以参考中文文档