logo头像
Snippet 博客主题

NO Title!

导航条相关

  1. 监听返回按钮
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/// 导航返回协议
@objc protocol NavigationProtocol {
/// 导航将要返回方法
///
/// - Returns: true: 返回上一界面, false: 禁止返回
@objc optional func navigationShouldPopMethod() -> Bool
}

extension UIViewController: NavigationProtocol {
func navigationShouldPopMethod() -> Bool {
return true
}
}

extension UINavigationController: UINavigationBarDelegate, UIGestureRecognizerDelegate {

public func navigationBar(_ navigationBar: UINavigationBar, shouldPop item: UINavigationItem) -> Bool {
if viewControllers.count < (navigationBar.items?.count)! {
return true
}
var shouldPop = false
let vc: UIViewController = topViewController!
if vc.responds(to: #selector(navigationShouldPopMethod)) {
shouldPop = vc.navigationShouldPopMethod()
}
if shouldPop {
DispatchQueue.main.async {
self.popViewController(animated: true)
}
} else {
for subview in navigationBar.subviews {
if 0.0 < subview.alpha && subview.alpha < 1.0 {
UIView.animate(withDuration: 0.25) {
subview.alpha = 1.0
}
}
}
}
return false
}

public func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
if childViewControllers.count == 1 {
return false
} else {
if topViewController?.responds(to: #selector(navigationShouldPopMethod)) != nil {
return topViewController!.navigationShouldPopMethod()
}
return true
}
}
}
  1. 多层级返回
1
2
let des = self.navigationController?.viewControllers[(self.navigationController?.viewControllers.index((self.navigationController?.viewControllers.endIndex)!, offsetBy: -4))!]
self.navigationController?.popToViewController(des!, animated: false)
  1. 属性设置
  • 设置返回按钮,只有一个箭头,或者自定义文字

    1
    self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: self, action: nil)
  • 去除底部黑线

    1
    self.navigationController?.navigationBar.shadowImage = UIImage()
  • 底部添加阴影

    1
    2
    3
    4
    5
    self.navigationController?.navigationBar.layer.shadowColor = COLOR_474744.cgColor
    self.navigationController?.navigationBar.layer.shadowOffset = CGSize(width: 0, height: 5)
    self.navigationController?.navigationBar.layer.shadowOpacity = 0.03
    self.navigationController?.navigationBar.layer.shadowRadius = 3
    self.navigationController?.navigationBar.layer.shadowPath = UIBezierPath.init(rect: (self.navigationController?.navigationBar.bounds)!).cgPath
  • 改变文字颜色

    1
    self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: COLOR_474744]
  • 改变背景颜色

    1
    2
    3
    self.navigationController?.navigationBar.setBackgroundImage(imageFromColor(color: COLOR_18ceb4), for: UIBarPosition.any, barMetrics: .default)
    或者用barTintColor
    self.navigationController?.navigationBar.barTintColor = COLOR_18ceb4
  • 改变透明

    1
    self.navigationController?.navigationBar.isTranslucent = false
  • 改变默认的BarButtonItem的颜色
    tintColor