UINavigationBarDecorator
兼容所有 iOS 版本的 UINavigationBarAppearance
特点
- 提供可更改大小的 AdvancedNavigationBar
- 可轻松应用于所有 iOS 版本的导航栏样式
- 将导航栏样式应用于所有 iOS 版本的单个界面
- 提供兼容 iOS 13 以下版本的 UINavigationBarAppearance
- 提供兼容的 UINavigationBarAppearance,类似于 iOS 13 以及以上版本上可用的 UINavigationBarAppearance
- 通过视图控制器生命周期自动应用外观到导航栏
演示
iOS 10.x 上的常规标题
iOS 11.x上的大标题
iOS 13及以上大标题
自定义NavigationBar
使用Interface Builder的高级NavigationBar
使用Interface Builder的PageSheetNavigationBar
使用编程方式
let navigationController = UINavigationController(
rootViewController: #{YourVC},
navigationBarClass: PageSheetNavigationBar.self)
UINavigationBarDecorator
使用工厂方式
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
}
}
不使用工厂方式
- 直接在视图控制器上设置装饰器
final class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
navigationBarDecorator = .init(standard: .orange, compact: .orange, scrollEdge: .orange)
}
}
手动应用装饰器
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)
}
}
示例
要运行示例项目,先克隆库,然后从示例目录运行 pod install
。
要求
至少 iOS SDK 10.0
安装
UINavigationBarDecorator 可通过 CocoaPods 获得。要安装,只需将以下行添加到你的 Podfile 中
pod "UINavigationBarDecorator"
Carthage
Carthage 是一个去中心化的依赖管理器,它构建您的依赖,并为您提供二进制框架。
您可以使用以下命令使用 Homebrew 安装 Carthage:
$ brew update
$ brew install carthage
要使用 Carthage 将 Alamofire 集成到您的 Xcode 项目中,请在您的 Cartfile
中指定它。
github "pisces/UINavigationBarDecorator" ~> 1.0.5
运行 carthage update
构建框架,并将构建的 UINavigationBarDecorator.framework
拖入您的 Xcode 项目。
作者
Steve Kim,[email protected]
许可证
UINavigationBarDecorator 适用于 BSD 许可证。有关更多信息,请参阅 LICENSE 文件。