EachNavigationBar
预览
用法
开启
给当前导航栈的 viewControllers 添加一个独立的导航栏。
只对当前导航栈有效,不影响其他导航栈。
Swift
let nav = UINavigationController(rootViewController: vc)
nav.navigation.configuration.isEnabled = true
Objective-C
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
nav.navigation_configuration.isEnabled = YES;
设置
导航栈全局配置
更多配置请看这里
Swift
nav.navigation.configuration.titleTextAttributes = [.foregroundColor: UIColor.blue]
nav.navigation.configuration.barTintColor = UIColor.red
nav.navigation.configuration.shadowImage = UIImage(named: "shadow")
nav.navigation.configuration.backBarButtonItem = .init(style: .image(UIImage(named: "back")), tintColor: UIColor.red)
nav.navigation.configuration.setBackgroundImage(UIImage(named: "nav"), for: .any, barMetrics: .default)
Objective-C
nav.navigation_configuration.titleTextAttributes = @{NSForegroundColorAttributeName: UIColor.blueColor};
nav.navigation_configuration.barTintColor = UIColor.redColor;
nav.navigation_configuration.shadowImage = [UIImage imageNamed:@"shadow"];
nav.navigation_configuration.backBarButtonItem = [[BackBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"]];
[nav.navigation_configuration setBackgroundImage:[UIImage imageNamed:@"nav"] for:UIBarPositionAny barMetrics:UIBarMetricsDefault];
每个控制器设置
普通设置
Swift
// 一般用法同系统组件
navigation.bar -> EachNavigationBar -> UINavigationBar
navigation.item -> UINavigationItem
// 隐藏导航栏
navigation.bar.isHidden = true
// 设置导航栏透明度
navigation.bar.alpha = 0.5
// 设置标题透明度
navigation.bar.setTitleAlpha(0.5)
// 设置 barButtonItem 透明度
navigation.bar.setTintAlpha(0.5)
// 如果 barButtonItem 是自定义视图
navigation.item.leftBarButtonItem?.customView?.alpha = 0.5
// 如果 barButtonItem 自定义了 tintColor
navigation.item.leftBarButtonItem?.tintColor = navigation.item.leftBarButtonItem?.tintColor?.withAlphaComponent(0.5)
// 移除毛玻璃效果
navigation.bar.isTranslucent = false
// 隐藏底部阴影
navigation.bar.isShadowHidden = true
// 设置状态栏样式
navigation.bar.statusBarStyle = .lightContent
// 设置返回按钮
navigation.bar.backBarButtonItem = .init(style: .title("Back"), tintColor: .red)
// 允许返回事件
navigation.bar.backBarButtonItem.shouldBack = { item in
// do something
return false
}
// 返回事件之前回调
navigation.bar.backBarButtonItem.willBack = {
// do something
}
// 返回事件之后回调
navigation.bar.backBarButtonItem.didBack = {
// do something
}
// 如果想自定义导航栏位置,请将下面属性设为 false
navigation.bar.automaticallyAdjustsPosition = false
// 导航栏附加高度
navigation.bar.additionalHeight = 14
// 导航栏附加视图
navigation.bar.additionalView = UIView()
// 导航栏阴影
navigation.bar.shadow = Shadow(
color: UIColor.black.cgColor,
opacity: 0.5,
offset: CGSize(width: 0, height: 3))
Objective-C
self.navigation_bar.xxx
self.navigation_item.xxx
大标题设置(iOS 11.0+)
UINavigationController
// 开启大标题
if #available(iOS 11.0, *) {
nav.navigation.prefersLargeTitles()
}
UIViewController
// 显示大标题
if #available(iOS 11.0, *) {
navigation.bar.prefersLargeTitles = true
}
// 隐藏大标题
if #available(iOS 11.0, *) {
navigation.bar.prefersLargeTitles = false
}
// 设置大标题透明度
if #available(iOS 11.0, *) {
navigation.bar.setLargeTitleAlpha(0.5)
}
关于约束
和使用系统导航栏完全一致。