Skip to content

pisces/UINavigationBarDecorator

Repository files navigation

UINavigationBarDecorator

Swift CI Status Version License Platform Carthage Compatible

Compatible UINavigationBarAppearance for all iOS versions

Features

  • Provide AdvancedNavigationBar is able to change size
  • Easy to apply navigation bar style for all iOS versions
  • Apply navigation bar style as one interface for all iOS versions
  • Provide compatible UINavigationBarAppearance for versions below iOS 13
  • Provide CompatibleNavigationBarAppearance like UINavigationBarAppearance that is available on iOS 13 higher
  • Automatically apply appearance to navigation bar through the view controller life cycle

Demo

Normal title on iOS 10.x

Large title on iOS 11.x

Large title on iOS 13 higher

Custom NavigationBar

AdvancedNavigationBar with Interface Builder

PageSheetNavigationBar with Interface Builder

Using Programacally

let navigationController = UINavigationController(
    rootViewController: #{YourVC}, 
    navigationBarClass: PageSheetNavigationBar.self)

UINavigationBarDecorator

Using with factory

class AppDelegate: UIResponder, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        UINavigationBarDecorator.isAllowsSwizzleLifeCycleOfViewController = true
        UINavigationBarDecorator.factory = SampleNavigationBarDecoratorFactory()
        return true
    }
}
struct SampleNavigationBarDecoratorFactory: UINavigationBarDecoratorFactory {
    func create(of vc: UIViewController) -> UINavigationBarDecorator? {
        switch vc {
        case is RootViewController:
            return .init(standard: .orange, compact: .orange, scrollEdge: .orange)
        case is SecondViewController:
            return .init(standard: .purple, compact: .purple, scrollEdge: .purple)
        default:
            return nil
        }
    }
}

extension CompatibleNavigationBarAppearance {
    static var purple: CompatibleNavigationBarAppearance {
        let appearance = CompatibleNavigationBarAppearance()
        appearance.backgroundColor = .purple
        appearance.tintColor = .white
        appearance.largeTitleTextAttributes = [
            .foregroundColor: UIColor.white
        ]
        appearance.titleTextAttributes = [
            .foregroundColor: UIColor.white,
            .font : UIFont.systemFont(ofSize: 17, weight: .semibold)
        ]
        return appearance
    }
    static var orange: CompatibleNavigationBarAppearance {
        let appearance = CompatibleNavigationBarAppearance()
        appearance.backgroundColor = .orange
        appearance.tintColor = .black
        appearance.largeTitleTextAttributes = [
            .foregroundColor: UIColor.black
        ]
        appearance.titleTextAttributes = [
            .foregroundColor: UIColor.black,
            .font : UIFont.systemFont(ofSize: 17, weight: .semibold)
        ]
        return appearance
    }
}

Using without factory

  • set the decorator directly on the view controller
final class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationBarDecorator = .init(standard: .orange, compact: .orange, scrollEdge: .orange)
    }
}

Apply decorator manually

class AppDelegate: UIResponder, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        UINavigationBarDecorator.isAllowsSwizzleLifeCycleOfViewController = false
        UINavigationBarDecorator.factory = SampleNavigationBarDecoratorFactory()
        return true
    }
}

final class SampleNavigationController: UINavigationController {
    override var childForStatusBarStyle: UIViewController? {
        topViewController
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        delegate = self
    }
}

extension SampleNavigationController: UINavigationControllerDelegate {
    func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
        viewController.navigationBarDecorator?.decorate(to: viewController)
    }
    func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
        viewController.navigationBarDecorator?.decorate(to: viewController, by: viewController.view.frame.size)
    }
}

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

iOS SDK 9.0 equal or higher

Installation

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

pod "UINavigationBarDecorator"

Carthage

Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.

You can install Carthage with Homebrew using the following command:

$ brew update
$ brew install carthage

To integrate Alamofire into your Xcode project using Carthage, specify it in your Cartfile:

github "pisces/UINavigationBarDecorator" ~> 1.0.8

Run carthage update to build the framework and drag the built UINavigationBarDecorator.framework into your Xcode project.

Author

Steve Kim, hh963103@gmail.com

License

UINavigationBarDecorator is available under the BSD license. See the LICENSE file for more info.