Skip to content

wlgemini/SwiftyNavigationBar

Repository files navigation

SwiftyNavigationBar

SwiftyNavigationBar CI Version License Platform

An easy way to customizing NavigationBar.

Quick Start

首先,你需要用以下extension中的一个init来初始化UINavigationController:

// 1
init(preference: ((Style) -> Void)?)

// 2
init(rootViewController: UIViewController, preference: ((Style) -> Void)?)

// 3
init(viewControllers: [UIViewController], preference: ((Style) -> Void)?)

// 4
init(viewControllers: [UIViewController], toolbarClass: AnyClass?, preference: ((Style) -> Void)?)
let nav = UINavigationController(rootViewController: ViewController(), preference: nil)

⚠️注意,以上init方法内部接管了UINavigationControllerdelegate,请勿再重新设置delegate。如果有需要,可以通过nav.snb.navigationControllerDelegate来设置delegate。

接下来,你只要需要在UIViewControllerviewDidLoad()或调用更靠前的方法中做一些配置就好,这些配置项都以snb为前缀:

override func viewDidLoad() {
    super.viewDidLoad()
    
    self.snb.style.backgroundEffect = .color(.red)
    self.snb.style.backgroundAlpha = 0.5
    ...
}

当你想更新当前NavigationBar的样式时,需要调用snb.updateStyle()方法:

self.snb.updateStyle { (style) in
	style.isWhiteBarStyle = true
	style.shadowImageAlpha = 0.5
}

⚠️这里需要注意的是,snb.updateStyle()只作用于UINavigationController.topViewController,你当前的ViewController在栈顶时才会起作用。

Style

具体来说有以下样式可配置(PS: 这些GIF当中的样式都是通过snb.updateStyle()方法更新的):

  • backgroundEffect:改变背景样式,有Blur/Image/Color三类样式可选

    backgroundEffect

  • backgroundAlpha:改变背景透明度(并不是改变NavigationBar的透明度)

    backgroundAlpha

  • tintColor:改变NavigationBartintColor,主要影响左右两边BarButtonItem的颜色

    tintColor

  • isWhiteBarStyle:改变NavigationBarbarStyle,主要影响StatusBartitle

    isWhiteBarStyle

  • shadowImageAlpha:改变shadowImage的透明度

    shadowImageAlpha

  • alpha:改变NavigationBar的透明度(这会使得整个NavigationBar被隐藏掉)

    alpha

样式的综合使用

  • 🌰#1

    use1

  • 🌰#2

    use2

推荐用法

你并不需要在每个UIViewControllerviewDidLoad()方法中都进行一番相关样式的配置。

实际上,SwiftyNavigationBar提供了3种样式配置的作用域,选择一个合适的作用域,会使配置更便捷:

  • UIViewController作用域:在ViewController中,以snb.style开头的相关属性配置,它只会影响当前ViewController的样式

    override func viewDidLoad() {
        ...
        self.snb.style.backgroundAlpha = /* alpha */
        ...
    }
  • UINavigationController作用域:会作用于UINavigationController中的所有viewControllers

    let nav = UINavigationController(rootViewController: ViewController(), preference: { (style) in
        // 样式配置
        style.backgroundEffect = /* effect */
        ...
    })
  • Global作用域:会作用于所有通过相关init方法初始化的UINavigationController

    SwiftyNavigationBar.Style.backgroundEffect = /* effect */
    SwiftyNavigationBar.Style.tintColor = /* tintColor */
    SwiftyNavigationBar.Style.alpha = /* alpha */

他们的优先级是:UIViewController作用域 > UINavigationController作用域 > Global作用域

其中Global作用域有默认值,也就是说,UINavigationController作用域/UIViewController作用域可以不用配置或只配置一部分样式,剩下的样式配置使用Global作用域的配置即可。

所以一个比较推荐的用法如下:

// 第一步,配置Global作用域
SwiftyNavigationBar.Style.backgroundEffect = /* effect */

// 第二步,配置UINavigationController作用域
let nav1 = UINavigationController(rootViewController: ViewController(), preference: { (style) in
    style.backgroundEffect = /* effect */
    style.tintColor = /* tintColor */
})

let nav2 = UINavigationController(rootViewController: ViewController(), preference: nil)
nav2.snb.navigationControllerDelegate = self /* 等价于nav.delegate = self */

// 第三步,配置UIViewController作用域
override func viewDidLoad() {
    ...
    self.snb.style.backgroundAlpha = /* alpha */
    ...
}

Requirements

  • iOS 8.0+
  • Swift 5.0

Installation

CocoaPods

SwiftyNavigationBar is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'SwiftyNavigationBar'

Swift Package Manager

From Xcode 11, you can use Swift Package Manager to add SwiftyNavigationBar to your project.

dependencies: [
    .package(url: "https://github.com/wlgemini/SwiftyNavigationBar.git", .upToNextMajor(from: "5.1.0"))
]

License

SwiftyNavigationBar is available under the MIT license. See the LICENSE file for more info.

About

An easy way to customizing NavigationBar.

Resources

License

Stars

Watchers

Forks

Packages

No packages published