Skip to content

Commit 24061ab

Browse files
committed
add UIViewController+Man.swift UIViewController+Find.swift UIViewController+UIViewController+NavigationBar.swift
1 parent 3accd1c commit 24061ab

File tree

4 files changed

+299
-0
lines changed

4 files changed

+299
-0
lines changed

SwiftMan.xcodeproj/project.pbxproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@
3838
DDA2A8BC1CD6EDAF00AAAB8C /* UIColor+Man.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDA2A8AE1CD6EDAF00AAAB8C /* UIColor+Man.swift */; };
3939
DDA2A8BD1CD6EDAF00AAAB8C /* UIImage+Man.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDA2A8B11CD6EDAF00AAAB8C /* UIImage+Man.swift */; };
4040
DDA2A8BE1CD6EDAF00AAAB8C /* UIScreen+Man.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDA2A8B61CD6EDAF00AAAB8C /* UIScreen+Man.swift */; };
41+
DDAEC86C1D3D1A210038F7EA /* UIViewController+Man.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDAEC86B1D3D1A210038F7EA /* UIViewController+Man.swift */; };
42+
DDAEC86E1D3D1A320038F7EA /* UIViewController+Find.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDAEC86D1D3D1A320038F7EA /* UIViewController+Find.swift */; };
43+
DDAEC8701D3D33890038F7EA /* UIViewController+UIViewController+NavigationBar.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDAEC86F1D3D33890038F7EA /* UIViewController+UIViewController+NavigationBar.swift */; };
4144
DDAEEA971D3C7C7000525126 /* UIView+Rect.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDAEEA961D3C7C7000525126 /* UIView+Rect.swift */; };
4245
DDAEEA991D3C9BC300525126 /* UIView+Man.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDAEEA981D3C9BC300525126 /* UIView+Man.swift */; };
4346
DDAEEA9B1D3C9C5400525126 /* UIView+Find.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDAEEA9A1D3C9C5400525126 /* UIView+Find.swift */; };
@@ -94,6 +97,9 @@
9497
DDA2A8AE1CD6EDAF00AAAB8C /* UIColor+Man.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIColor+Man.swift"; sourceTree = "<group>"; };
9598
DDA2A8B11CD6EDAF00AAAB8C /* UIImage+Man.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIImage+Man.swift"; sourceTree = "<group>"; };
9699
DDA2A8B61CD6EDAF00AAAB8C /* UIScreen+Man.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIScreen+Man.swift"; sourceTree = "<group>"; };
100+
DDAEC86B1D3D1A210038F7EA /* UIViewController+Man.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIViewController+Man.swift"; sourceTree = "<group>"; };
101+
DDAEC86D1D3D1A320038F7EA /* UIViewController+Find.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIViewController+Find.swift"; sourceTree = "<group>"; };
102+
DDAEC86F1D3D33890038F7EA /* UIViewController+UIViewController+NavigationBar.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIViewController+UIViewController+NavigationBar.swift"; sourceTree = "<group>"; };
97103
DDAEEA961D3C7C7000525126 /* UIView+Rect.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIView+Rect.swift"; sourceTree = "<group>"; };
98104
DDAEEA981D3C9BC300525126 /* UIView+Man.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIView+Man.swift"; sourceTree = "<group>"; };
99105
DDAEEA9A1D3C9C5400525126 /* UIView+Find.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIView+Find.swift"; sourceTree = "<group>"; };
@@ -416,6 +422,9 @@
416422
DDA2A8B91CD6EDAF00AAAB8C /* UIViewController */ = {
417423
isa = PBXGroup;
418424
children = (
425+
DDAEC86B1D3D1A210038F7EA /* UIViewController+Man.swift */,
426+
DDAEC86D1D3D1A320038F7EA /* UIViewController+Find.swift */,
427+
DDAEC86F1D3D33890038F7EA /* UIViewController+UIViewController+NavigationBar.swift */,
419428
);
420429
path = UIViewController;
421430
sourceTree = "<group>";
@@ -580,6 +589,7 @@
580589
DDED9EEB1CD9CCC000E196EB /* UIWindow+Man.swift in Sources */,
581590
DDED9EE41CD9A39900E196EB /* NSTimer+Addition.swift in Sources */,
582591
DDED9F001CDA0F1900E196EB /* Dictionary+Man.swift in Sources */,
592+
DDAEC8701D3D33890038F7EA /* UIViewController+UIViewController+NavigationBar.swift in Sources */,
583593
DDED9EFC1CD9FC7100E196EB /* String+URL.swift in Sources */,
584594
DDED9EFE1CDA0F0000E196EB /* Array+Man.swift in Sources */,
585595
DD6651301CD90AA000C81C38 /* NSBundle+AppIcon.swift in Sources */,
@@ -596,12 +606,14 @@
596606
DDED9EEE1CD9D11800E196EB /* Bool+Man.swift in Sources */,
597607
DDED9EF01CD9D25800E196EB /* AppInfos.swift in Sources */,
598608
DD3F99B81D257ACD0097F5D3 /* UIAlertController+Man.swift in Sources */,
609+
DDAEC86C1D3D1A210038F7EA /* UIViewController+Man.swift in Sources */,
599610
DD6651321CD9103E00C81C38 /* NSObject+Man.swift in Sources */,
600611
DDA2A8BB1CD6EDAF00AAAB8C /* UIApplication+Man.swift in Sources */,
601612
DDA2A8BE1CD6EDAF00AAAB8C /* UIScreen+Man.swift in Sources */,
602613
DDAEEA971D3C7C7000525126 /* UIView+Rect.swift in Sources */,
603614
DDA2A8BD1CD6EDAF00AAAB8C /* UIImage+Man.swift in Sources */,
604615
DD66512E1CD8F5C800C81C38 /* NSBundle+Man.swift in Sources */,
616+
DDAEC86E1D3D1A320038F7EA /* UIViewController+Find.swift in Sources */,
605617
DD244E321CD7A286009CAB3B /* UIDevice+Man.swift in Sources */,
606618
DDAEEA991D3C9BC300525126 /* UIView+Man.swift in Sources */,
607619
);
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//
2+
// UIViewController+Find.swift
3+
// SwiftMan
4+
//
5+
// Created by neu on 16/7/18.
6+
// Copyright © 2016年 cactus. All rights reserved.
7+
//
8+
9+
10+
import UIKit
11+
12+
13+
extension UIViewController {
14+
15+
public func m_topMostController() -> UIViewController {
16+
var topController: UIViewController = self
17+
// Getting topMost ViewController
18+
while let presentedVC = self.presentedViewController {
19+
topController = presentedVC
20+
}
21+
// Returning topMost ViewController
22+
return topController
23+
}
24+
25+
public func m_currentViewController() -> UIViewController {
26+
var currentViewController: UIViewController = self.m_topMostController()
27+
while let topVC = (currentViewController as? UINavigationController)?.topViewController {
28+
currentViewController = topVC
29+
}
30+
return currentViewController
31+
}
32+
33+
public class func m_currentViewControllerFromcurrentView() -> UIViewController? {
34+
var result: UIViewController? = nil
35+
// 1. get current window
36+
guard var window = UIApplication.sharedApplication().keyWindow else {
37+
return nil
38+
}
39+
if window.windowLevel != UIWindowLevelNormal {
40+
let windows = UIApplication.sharedApplication().windows
41+
for tmpWin in windows {
42+
if tmpWin.windowLevel == UIWindowLevelNormal {
43+
window = tmpWin
44+
}
45+
}
46+
}
47+
// 2. get current View Controller
48+
let frontView = window.subviews[0]
49+
let nextResponder = frontView.nextResponder()
50+
if (nextResponder is UIViewController) {
51+
result = nextResponder as? UIViewController
52+
}
53+
else {
54+
result = window.rootViewController
55+
}
56+
return result
57+
}
58+
59+
public func m_isVisible() -> Bool {
60+
return self.isViewLoaded() && (self.view.window != nil)
61+
}
62+
63+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
//
2+
// UIViewController+Man.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+
extension UIViewController {
12+
13+
// MARK: - add / remove
14+
public func m_displayController(controller: UIViewController, frame: CGRect) {
15+
self.addChildViewController(controller)
16+
controller.view.frame = frame
17+
self.view!.addSubview(controller.view)
18+
controller.didMoveToParentViewController(self)
19+
}
20+
21+
public func m_hideController(controller: UIViewController) {
22+
controller.willMoveToParentViewController(nil)
23+
controller.view.removeFromSuperview()
24+
controller.removeFromParentViewController()
25+
}
26+
27+
// MARK: - status bar
28+
public var m_statusBarHeight: CGFloat {
29+
return self.view.window!.convertRect(UIApplication.sharedApplication().statusBarFrame, toView: self.view).size.height
30+
}
31+
32+
// MARK: - navigation bar
33+
public var m_navigationBarHeight: CGFloat {
34+
get {
35+
if let me = self as? UINavigationController,
36+
let visibleViewController = me.visibleViewController {
37+
return visibleViewController.m_navigationBarHeight
38+
}
39+
if let nav = self.navigationController {
40+
return nav.navigationBar.frame.size.height
41+
}
42+
return 0
43+
}
44+
}
45+
46+
47+
public var m_navigationBarColor: UIColor? {
48+
get {
49+
if let me = self as? UINavigationController,
50+
let visibleViewController = me.visibleViewController {
51+
return visibleViewController.m_navigationBarColor
52+
}
53+
return navigationController?.navigationBar.tintColor
54+
} set(value) {
55+
navigationController?.navigationBar.barTintColor = value
56+
}
57+
}
58+
59+
// MARK: - tab bar
60+
public var m_tabBarHeight: CGFloat {
61+
get {
62+
if let me = self as? UINavigationController,
63+
let visibleViewController = me.visibleViewController {
64+
return visibleViewController.m_tabBarHeight
65+
}
66+
if let tab = self.tabBarController {
67+
return tab.tabBar.frame.size.height
68+
}
69+
return 0
70+
}
71+
}
72+
73+
}
74+
75+
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
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

Comments
 (0)