-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUINavigationController+Extensions.swift
140 lines (119 loc) · 4.31 KB
/
UINavigationController+Extensions.swift
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//
// UINavigationController+Extensions.swift
// OYExtensions
//
// Created by osmanyildirim
//
import UIKit
extension UINavigationController {
/// Push ViewController with completion handler
public override func oy_push(viewControler: UIViewController, animated: Bool = true, completion: (() -> Void)? = nil) {
CATransaction.begin()
CATransaction.setCompletionBlock(completion)
pushViewController(viewControler, animated: true)
CATransaction.commit()
}
/// Pop ViewController with completion handler
public override func oy_pop(animated: Bool = true, completion: (() -> Void)? = nil) {
CATransaction.begin()
CATransaction.setCompletionBlock(completion)
popViewController(animated: animated)
CATransaction.commit()
}
/// Pop to root ViewController with completion handler
public override func oy_popToRoot(animated: Bool = true, completion: (() -> Void)? = nil) {
CATransaction.begin()
CATransaction.setCompletionBlock(completion)
popToRootViewController(animated: animated)
CATransaction.commit()
}
/// Pop to ViewController with completion handler
public override func oy_popTo(viewControler: UIViewController, animated: Bool = true, completion: (() -> Void)? = nil) {
CATransaction.begin()
CATransaction.setCompletionBlock(completion)
popToViewController(viewControler, animated: animated)
CATransaction.commit()
}
/// Show navigation bar
public func oy_show(animated: Bool = true) {
navigationController?.setNavigationBarHidden(false, animated: animated)
}
/// Hide navigation bar
public func oy_hide(animated: Bool = true) {
navigationController?.setNavigationBarHidden(true, animated: animated)
}
/// Hide navigation controller bars when the keyboard appears
public func oy_hideWhenKeyboardAppears(_ isHide: Bool) {
hidesBarsWhenKeyboardAppears = isHide
}
/// Get and Set translucent
public var oy_isTranslucent: Bool {
get {
navigationBar.isTranslucent
}
set(value) {
navigationBar.isTranslucent = value
}
}
/// Get and Set title
public var oy_title: String {
get {
navigationBar.oy_title
}
set(value) {
navigationBar.oy_title = value
}
}
/// Get and Set title color
public var oy_titleColor: UIColor? {
get {
navigationBar.oy_titleColor
}
set(value) {
navigationBar.oy_titleColor = value
}
}
/// Get and Set background color
public var oy_backgroundColor: UIColor? {
get {
navigationBar.oy_backgroundColor
}
set(value) {
navigationBar.oy_backgroundColor = value
}
}
/// Get and Set title font
public var oy_titleFont: UIFont? {
get {
navigationBar.oy_font
}
set(value) {
navigationBar.oy_font = value
}
}
/// UINavigationBar height
public var oy_height: CGFloat {
navigationBar.bounds.height
}
/// Change UINavigationBar's button colors
public func oy_changeButtonsColors(color: UIColor) {
UINavigationBar.appearance().tintColor = color
UINavigationBar.appearance().titleTextAttributes = [.foregroundColor: color]
}
}
extension UIViewController: UIGestureRecognizerDelegate {
private static var gesture = UIPanGestureRecognizer()
/// Enable pop gesture for UINavigationController
/// - Parameter fromAnywhere: if true will be pop from screen's anyhwere
public func oy_enablePopGesture(fromAnywhere: Bool = false) {
navigationController?.interactivePopGestureRecognizer?.isEnabled = true
guard fromAnywhere, let gesture = navigationController?.interactivePopGestureRecognizer, let targets = gesture.value(forKey: "targets") as? NSMutableArray else { return }
Self.gesture.setValue(targets, forKey: "targets")
view.addGestureRecognizer(Self.gesture)
}
/// Disable pop gesture for UINavigationController
public func oy_disablePopGesture() {
navigationController?.interactivePopGestureRecognizer?.isEnabled = false
view.removeGestureRecognizer(Self.gesture)
}
}