forked from zhiphe/Potatso-iOS
-
Notifications
You must be signed in to change notification settings - Fork 1
/
UIViewControllerExtensions.swift
103 lines (85 loc) · 3.17 KB
/
UIViewControllerExtensions.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
//
// UIViewControllerExtensions.swift
// Potatso
//
// Created by LEI on 12/12/15.
// Copyright © 2015 TouchingApp. All rights reserved.
//
import UIKit
import Aspects
extension UIViewController: UIGestureRecognizerDelegate {
public override class func initialize() {
struct Static {
static var token: dispatch_once_t = 0
}
// make sure this isn't a subclass
if self !== UIViewController.self {
return
}
dispatch_once(&Static.token) {
UIViewController.aspectHook(#selector(viewDidLoad), swizzledSelector: #selector(ics_viewDidLoad))
UIViewController.aspectHook(#selector(viewWillAppear(_:)), swizzledSelector: #selector(ics_viewWillAppear(_:)))
UIViewController.aspectHook(#selector(viewDidAppear(_:)), swizzledSelector: #selector(ics_viewDidAppear(_:)))
UIViewController.aspectHook(#selector(viewWillDisappear(_:)), swizzledSelector: #selector(ics_viewWillDisappear(_:)))
}
}
// MARK: - Method Swizzling
func ics_viewWillAppear(animated: Bool) {
self.ics_viewWillAppear(animated)
if let navVC = self.navigationController {
showLeftBackButton(navVC.viewControllers.count > 1)
}
}
func ics_viewDidLoad() {
self.ics_viewDidLoad()
}
func ics_viewDidAppear(animated: Bool) {
self.ics_viewDidAppear(animated)
if let navVC = self.navigationController {
enableSwipeGesture(navVC.viewControllers.count > 1)
}
}
func ics_viewWillDisappear(animated: Bool) {
self.ics_viewWillDisappear(animated)
}
func showLeftBackButton(shouldShow: Bool) {
if shouldShow {
let backItem = UIBarButtonItem(image: "Back".templateImage, style: UIBarButtonItemStyle.Plain, target: self, action: #selector(pop))
navigationItem.leftBarButtonItem = backItem
}else{
navigationItem.leftBarButtonItem = nil
}
}
func enableSwipeGesture(shouldShow: Bool) {
if shouldShow {
navigationController?.interactivePopGestureRecognizer?.delegate = self
navigationController?.interactivePopGestureRecognizer?.enabled = true
}else{
navigationController?.interactivePopGestureRecognizer?.delegate = nil
navigationController?.interactivePopGestureRecognizer?.enabled = false
}
}
func addChildVC(child: UIViewController) {
view.addSubview(child.view)
addChildViewController(child)
child.didMoveToParentViewController(self)
}
func removeChildVC(child: UIViewController) {
child.willMoveToParentViewController(nil)
child.view.removeFromSuperview()
child.removeFromParentViewController()
}
func pop() {
navigationController?.popViewControllerAnimated(true)
}
func dismiss() {
dismissViewControllerAnimated(true, completion: nil)
}
func close() {
if let navVC = self.navigationController where navVC.viewControllers.count > 1 {
pop()
}else {
dismiss()
}
}
}