1
+ //
2
+ // UIViewController+UIViewController+NavigationBar.swift
3
+ // SwiftMan
4
+ //
5
+ // Created by neu on 16/7/18.
6
+ // Copyright © 2016年 cactus. All rights reserved.
7
+ //
8
+
9
+ import UIKit
10
+
11
+
12
+ extension UIViewController {
13
+ private struct AssociatedKeys {
14
+ static var SwitchEffectKey = " SwitchEffectKey "
15
+ static var LeftActionBlockKey = " LeftActionBlockKey "
16
+ static var RightActionBlockKey = " RightActionBlockKey "
17
+ }
18
+
19
+ public enum NavigationBarButtonAsPosition : String {
20
+ case NavigationBarButtonAsLeft = " leftBarButtonAction: "
21
+ case NavigationBarButtonAsRight = " rightBarButtonAction: "
22
+ }
23
+ public typealias ActionBlock = @convention ( block) ( barButton: UIButton ? ) -> Void
24
+
25
+ public func m_setNavigationBarButtonPosition( position: NavigationBarButtonAsPosition ,
26
+ normalImage: UIImage ? ,
27
+ highlightedImage: UIImage ? ,
28
+ normalBgImage: UIImage ? ,
29
+ highlightedBgImage: UIImage ? ,
30
+ title: String ? ,
31
+ titleFont: UIFont ? ,
32
+ normalColor: UIColor ? ,
33
+ highlightedColor: UIColor ? ,
34
+ switchEffect: Bool ,
35
+ actionBlock: ActionBlock ? ,
36
+ offset: CGFloat , //set -24 to system default, toolkit provide 24 as default offset
37
+ setButtonBlock: ActionBlock ? ) {
38
+
39
+ let barButton = UIButton ( type: . Custom)
40
+ barButton. frame = CGRectMake ( 0 , 0 , 45 , 40 )
41
+ barButton. backgroundColor = UIColor . clearColor ( )
42
+ barButton. addTarget ( self , action: Selector ( position. rawValue) , forControlEvents: . TouchUpInside)
43
+ barButton. imageView? . contentMode = . ScaleAspectFit
44
+ if let image = normalImage{
45
+ barButton. setImage ( image, forState: . Normal)
46
+ }
47
+ if let image = highlightedImage{
48
+ barButton. setImage ( image, forState: . Highlighted)
49
+ }
50
+
51
+ if let image = normalBgImage{
52
+ barButton. setBackgroundImage ( image, forState: . Normal)
53
+ }
54
+ if let image = highlightedBgImage{
55
+ barButton. setBackgroundImage ( image, forState: . Highlighted)
56
+ }
57
+
58
+ if let titleTemp = title{
59
+ barButton. setTitle ( titleTemp, forState: . Normal)
60
+ }
61
+ if let font = titleFont{
62
+ barButton. titleLabel? . font = font
63
+ }
64
+ if let color = normalColor{
65
+ barButton. setTitleColor ( color, forState: . Normal)
66
+ }
67
+ if let color = highlightedColor{
68
+ barButton. setTitleColor ( color, forState: . Highlighted)
69
+ }
70
+
71
+ if ( switchEffect) {
72
+ if let image = highlightedImage{
73
+ barButton. setImage ( image, forState: . Selected)
74
+ }
75
+ if let image = highlightedBgImage{
76
+ barButton. setBackgroundImage ( image, forState: . Selected)
77
+ }
78
+ if let color = highlightedColor{
79
+ barButton. setTitleColor ( color, forState: . Selected)
80
+ }
81
+ }
82
+
83
+ let barButtonItem = UIBarButtonItem ( customView: barButton)
84
+ if position == . NavigationBarButtonAsLeft{
85
+ self . navigationItem. leftBarButtonItem = barButtonItem;
86
+ } else {
87
+ self . navigationItem. rightBarButtonItem = barButtonItem;
88
+ }
89
+ barButton. imageEdgeInsets = UIEdgeInsetsMake ( 0 , position == . NavigationBarButtonAsLeft ? - ( 24 + offset) : 0 , 0 , position == . NavigationBarButtonAsLeft ? 0 : - ( 24 + offset) ) ;
90
+ barButton. titleEdgeInsets = UIEdgeInsetsMake ( 0 , position == . NavigationBarButtonAsLeft ? - ( 24 + offset) : 0 , 0 , position == . NavigationBarButtonAsLeft ? 0 : - ( 24 + offset) ) ;
91
+ if let blockTemp = setButtonBlock {
92
+ blockTemp ( barButton: barButton) ;
93
+ }
94
+ if ( switchEffect) {
95
+ objc_setAssociatedObject ( self , & AssociatedKeys. SwitchEffectKey, true , objc_AssociationPolicy. OBJC_ASSOCIATION_ASSIGN)
96
+ }
97
+
98
+ //http://www.hmttommy.com/2015/12/11/AddCategoryProperty/
99
+ if let block = actionBlock{
100
+ let wrapper = unsafeBitCast ( block, AnyObject . self)
101
+ if position == . NavigationBarButtonAsLeft{
102
+ objc_setAssociatedObject ( self , & AssociatedKeys. LeftActionBlockKey, wrapper, objc_AssociationPolicy. OBJC_ASSOCIATION_RETAIN_NONATOMIC)
103
+ } else {
104
+ objc_setAssociatedObject ( self , & AssociatedKeys. RightActionBlockKey, wrapper, objc_AssociationPolicy. OBJC_ASSOCIATION_RETAIN_NONATOMIC)
105
+ }
106
+
107
+ }
108
+
109
+
110
+ }
111
+
112
+ func leftBarButtonAction( sender: UIButton ? )
113
+ {
114
+ let switchEffect = objc_getAssociatedObject ( self , & AssociatedKeys. SwitchEffectKey) as? Bool
115
+ if let _ = switchEffect {
116
+ if let button = sender{
117
+ button. selected = !button. selected
118
+ }
119
+ }
120
+
121
+ let wrapper = objc_getAssociatedObject ( self , & AssociatedKeys. LeftActionBlockKey)
122
+ if wrapper == nil {
123
+ return
124
+ }
125
+ let block = unsafeBitCast ( wrapper, ActionBlock . self)
126
+ block ( barButton: sender)
127
+ }
128
+
129
+ func rightBarButtonAction( sender: UIButton ? )
130
+ {
131
+ let switchEffect = objc_getAssociatedObject ( self , & AssociatedKeys. SwitchEffectKey) as? Bool
132
+ if let _ = switchEffect {
133
+ if let button = sender{
134
+ button. selected = !button. selected
135
+ }
136
+ }
137
+
138
+
139
+ let wrapper = objc_getAssociatedObject ( self , & AssociatedKeys. RightActionBlockKey)
140
+ if wrapper == nil {
141
+ return
142
+ }
143
+ let block = unsafeBitCast ( wrapper, ActionBlock . self)
144
+ block ( barButton: sender)
145
+
146
+
147
+ }
148
+
149
+ }
0 commit comments