微信导航栏
WXNavigationBar
可通过 CocoaPods 获得。要安装它,只需将以下行添加到您的 Podfile 中:
use_frameworks !
pod ' WXNavigationBar ' , ' ~> 2.3.6 '
Carthage 是一个去中心化的依赖管理器,它可以构建您的依赖项并为您提供二进制框架。要使用 Carthage 将 WXNavigationBar 集成到您的 Xcode 项目中,请在您的 Cartfile 中指定它:
github alexiscn/WXNavigationBar
Swift Package Manager 是一个用于自动分发 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
属性。
使用WXNavigationBar.NavBar
配置WXNavigationBar
将会影响所有的viewcontroller。
在你的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
您还可以通过覆盖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
}
通过设置wx_barBarTintColor
,您实际上设置了navigationController?.navigationBar
的barTintColor
override var wx_barBarTintColor : UIColor ? {
return . red
}
通过设置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
}
override wx_interactivePopMaxAllowedDistanceToLeftEdge : CGFloat {
return view . bounds . width * 0.5
}
以下是WXNavigationBar
的一些高级使用建议。
有多种方法可以使导航栏透明。
wx_navigationBar . alpha = 0
wx_navigationBar . isHidden = true
override var wx_navigationBarBackgroundColor : UIColor ? {
return . clear
}
alpha和hidden使wx_navigationBar不可见,而backgroundColor只是改变wx_navigationBar的颜色
您可以动态更新导航栏,例如滚动时动态更新。
有关详细信息,请参阅MomentViewController
。
wx_navigationBar
是UIView的子类,所以你可以用wx_navigationBar
做任何UIView能做的事情。
如果你想在用户点击后退按钮时执行某些操作,你可以在视图控制器中重写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 许可。执照
你可以参考中文文档