Skip to content

Commit 1aac4a7

Browse files
committedAug 15, 2016
Move viewFromNib into SwiftMessages
This avoids the awkward naving conventions needed for extensions UIView and there is no real advantage to the methods being defined on UIView.
1 parent 27f94ed commit 1aac4a7

21 files changed

+259
-233
lines changed
 

‎Demo/Demo/ExploreViewController.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class ExploreViewController: UITableViewController, UITextFieldDelegate {
2222
case 2:
2323
view = MessageView.viewFromNib(layout: .StatusLine)
2424
default:
25-
view = try! MessageView.viewFromNib()
25+
view = try! SwiftMessages.viewFromNib()
2626
}
2727

2828
view.configureContent(title: titleText.text, body: bodyText.text, iconImage: nil, iconText: nil, buttonImage: nil, buttonTitle: "Hide", buttonTapHandler: { _ in SwiftMessages.hide() })

‎Demo/Demo/ViewController.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ class ViewController: UITableViewController {
114114
}
115115

116116
static func demoCustomNib() {
117-
let view: TacoDialogView = try! TacoDialogView.viewFromNib()
117+
let view: TacoDialogView = try! SwiftMessages.viewFromNib()
118118
view.configureDropShadow()
119119
view.getTacosAction = { _ in SwiftMessages.hide() }
120120
view.cancelAction = { SwiftMessages.hide() }

‎Demo/Pods/Pods.xcodeproj/project.pbxproj

+118-122
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎SwiftMessages.xcodeproj/project.pbxproj

+1-5
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
86BBA9071D5E040C00FE8F16 /* MarginAdjustable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 86AAF81D1D5549680031EE32 /* MarginAdjustable.swift */; };
2525
86BBA9081D5E040C00FE8F16 /* Error.swift in Sources */ = {isa = PBXBuildFile; fileRef = 86AAF82A1D580DD70031EE32 /* Error.swift */; };
2626
86BBA90F1D5E428000FE8F16 /* DropShadowView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 86BBA90E1D5E428000FE8F16 /* DropShadowView.swift */; };
27-
86D9B3561D5E79D100EFF424 /* UIView+SwiftMessages.swift in Sources */ = {isa = PBXBuildFile; fileRef = 86D9B3551D5E79D100EFF424 /* UIView+SwiftMessages.swift */; };
2827
/* End PBXBuildFile section */
2928

3029
/* Begin PBXContainerItemProxy section */
@@ -63,7 +62,6 @@
6362
86B48AFC1D5A41C900063E2B /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
6463
86BBA8F81D5E01FC00FE8F16 /* CardView.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; name = CardView.xib; path = Resources/CardView.xib; sourceTree = "<group>"; };
6564
86BBA90E1D5E428000FE8F16 /* DropShadowView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DropShadowView.swift; sourceTree = "<group>"; };
66-
86D9B3551D5E79D100EFF424 /* UIView+SwiftMessages.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIView+SwiftMessages.swift"; sourceTree = "<group>"; };
6765
/* End PBXFileReference section */
6866

6967
/* Begin PBXFrameworksBuildPhase section */
@@ -111,9 +109,8 @@
111109
86AAF82C1D580F410031EE32 /* Icon.swift */,
112110
864495551D4F7C390056EB2A /* Identifiable.swift */,
113111
86AAF81D1D5549680031EE32 /* MarginAdjustable.swift */,
114-
86AAF82A1D580DD70031EE32 /* Error.swift */,
115112
86BBA90E1D5E428000FE8F16 /* DropShadowView.swift */,
116-
86D9B3551D5E79D100EFF424 /* UIView+SwiftMessages.swift */,
113+
86AAF82A1D580DD70031EE32 /* Error.swift */,
117114
);
118115
name = Base;
119116
sourceTree = "<group>";
@@ -299,7 +296,6 @@
299296
86BBA9041D5E040600FE8F16 /* NSBundle+Utils.swift in Sources */,
300297
86BBA8FD1D5E03F800FE8F16 /* SwiftMessages.swift in Sources */,
301298
86BBA9021D5E040600FE8F16 /* WindowViewController.swift in Sources */,
302-
86D9B3561D5E79D100EFF424 /* UIView+SwiftMessages.swift in Sources */,
303299
86BBA8FF1D5E040600FE8F16 /* Presenter.swift in Sources */,
304300
86BBA90F1D5E428000FE8F16 /* DropShadowView.swift in Sources */,
305301
86BBA9051D5E040C00FE8F16 /* Icon.swift in Sources */,

‎SwiftMessages/DropShadowView.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// DropShadowView.swift
33
// SwiftMessages
44
//
5-
// Created by Tim Moose on 8/12/16.
6-
// Copyright © 2016 SwiftKick Mobile. All rights reserved.
5+
// Created by Timothy Moose on 8/12/16.
6+
// Copyright © 2016 SwiftKick Mobile LLC. All rights reserved.
77
//
88

99
import UIKit

‎SwiftMessages/Error.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// Error.swift
33
// SwiftMessages
44
//
5-
// Created by Tim Moose on 8/7/16.
6-
// Copyright © 2016 SwiftKick Mobile. All rights reserved.
5+
// Created by Timothy Moose on 8/7/16.
6+
// Copyright © 2016 SwiftKick Mobile LLC. All rights reserved.
77
//
88

99
import Foundation

‎SwiftMessages/Icon.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// SwiftMessages
44
//
55
// Created by Timothy Moose on 8/7/16.
6-
// Copyright © 2016 SwiftKick Mobile LLC. All rights reserved.
6+
// Copyright © 2016 SwiftKick Mobile LLC.LLC. All rights reserved.
77
//
88

99
import UIKit

‎SwiftMessages/Identifiable.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// Identifiable.swift
33
// SwiftMessages
44
//
5-
// Created by Tim Moose on 8/1/16.
6-
// Copyright © 2016 SwiftKick Mobile. All rights reserved.
5+
// Created by Timothy Moose on 8/1/16.
6+
// Copyright © 2016 SwiftKick Mobile LLC. All rights reserved.
77
//
88

99
import Foundation

‎SwiftMessages/MarginAdjustable.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// SwiftMessages
44
//
55
// Created by Timothy Moose on 8/5/16.
6-
// Copyright © 2016 SwiftKick Mobile LLC. All rights reserved.
6+
// Copyright © 2016 SwiftKick Mobile LLC.LLC. All rights reserved.
77
//
88

99
import UIKit

‎SwiftMessages/MessageView.swift

+70-31
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,59 @@
33
// SwiftMessages
44
//
55
// Created by Timothy Moose on 7/30/16.
6-
// Copyright © 2016 SwiftKick Mobile LLC. All rights reserved.
6+
// Copyright © 2016 SwiftKick Mobile LLC.LLC. All rights reserved.
77
//
88

99
import UIKit
1010

1111
public class MessageView: DropShadowView, Identifiable, MarginAdjustable {
12-
12+
1313
/*
14-
MARK: - IB outlets
14+
MARK: - Creating message views
1515
*/
1616

17-
@IBOutlet public var titleLabel: UILabel?
18-
@IBOutlet public var bodyLabel: UILabel?
19-
@IBOutlet public var iconContainer: UIView?
20-
@IBOutlet public var iconImageView: UIImageView?
21-
@IBOutlet public var iconLabel: UILabel?
22-
@IBOutlet public var contentView: UIView?
23-
@IBOutlet public var backgroundView: UIView!
17+
public enum Layout: String {
18+
case MessageView = "MessageView"
19+
case CardView = "CardView"
20+
case StatusLine = "StatusLine"
21+
case MessageViewIOS8 = "MessageViewIOS8"
22+
}
2423

25-
@IBOutlet public var button: UIButton? {
24+
public static func viewFromNib<T: MessageView>(layout layout: Layout, filesOwner: AnyObject = NSNull.init()) -> T {
25+
return try! SwiftMessages.viewFromNib(named: layout.rawValue)
26+
}
27+
28+
public static func viewFromNib<T: MessageView>(layout layout: Layout, bundle: NSBundle, filesOwner: AnyObject = NSNull.init()) -> T {
29+
return try! SwiftMessages.viewFromNib(named: layout.rawValue, bundle: bundle, filesOwner: filesOwner)
30+
}
31+
32+
/*
33+
MARK: - Tap handler
34+
*/
35+
36+
public var tapHandler: ((view: MessageView) -> Void)? {
2637
didSet {
27-
if let old = oldValue {
28-
old.removeTarget(self, action: #selector(MessageView.buttonTapped(_:)), forControlEvents: .TouchUpInside)
29-
}
30-
if let button = button {
31-
button.addTarget(self, action: #selector(MessageView.buttonTapped(_:)), forControlEvents: .TouchUpInside)
32-
}
38+
installTapRecognizer()
39+
}
40+
}
41+
42+
private lazy var tapRecognizer: UITapGestureRecognizer = {
43+
let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(MessageView.tapped))
44+
return tapRecognizer
45+
}()
46+
47+
func tapped() {
48+
tapHandler?(view: self)
49+
}
50+
51+
private func installTapRecognizer() {
52+
guard let contentView = contentView else { return }
53+
contentView.removeGestureRecognizer(tapRecognizer)
54+
if tapHandler != nil {
55+
// Only install the tap recognizer if there is a tap handler,
56+
// which makes it slightly nicer if one wants to install
57+
// a custom gesture recognizer.
58+
contentView.addGestureRecognizer(tapRecognizer)
3359
}
3460
}
3561

@@ -44,22 +70,35 @@ public class MessageView: DropShadowView, Identifiable, MarginAdjustable {
4470
}
4571

4672
/*
47-
MARK: - Creating message views
73+
MARK: - IB outlets
4874
*/
4975

50-
public enum Layout: String {
51-
case MessageView = "MessageView"
52-
case CardView = "CardView"
53-
case StatusLine = "StatusLine"
54-
case MessageViewIOS8 = "MessageViewIOS8"
55-
}
56-
57-
public static func viewFromNib<T: MessageView>(layout layout: Layout) -> T {
58-
return try! UIView.viewFromNib(named: layout.rawValue)
76+
@IBOutlet public var titleLabel: UILabel?
77+
@IBOutlet public var bodyLabel: UILabel?
78+
@IBOutlet public var iconContainer: UIView?
79+
@IBOutlet public var iconImageView: UIImageView?
80+
@IBOutlet public var iconLabel: UILabel?
81+
82+
@IBOutlet public var contentView: UIView! {
83+
didSet {
84+
if let old = oldValue {
85+
old.removeGestureRecognizer(tapRecognizer)
86+
}
87+
installTapRecognizer()
88+
}
5989
}
60-
61-
public static func viewFromNib<T: MessageView>(layout layout: Layout, bundle: NSBundle) -> T {
62-
return try! UIView.viewFromNib(named: layout.rawValue, bundle: bundle)
90+
91+
@IBOutlet public var backgroundView: UIView!
92+
93+
@IBOutlet public var button: UIButton? {
94+
didSet {
95+
if let old = oldValue {
96+
old.removeTarget(self, action: #selector(MessageView.buttonTapped(_:)), forControlEvents: .TouchUpInside)
97+
}
98+
if let button = button {
99+
button.addTarget(self, action: #selector(MessageView.buttonTapped(_:)), forControlEvents: .TouchUpInside)
100+
}
101+
}
63102
}
64103

65104
/*
@@ -98,7 +137,7 @@ public class MessageView: DropShadowView, Identifiable, MarginAdjustable {
98137
}
99138

100139
/*
101-
MARK: - Configuring the theme
140+
MARK: - Theming the view
102141
*/
103142

104143
extension MessageView {

‎SwiftMessages/NSBundle+Utils.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// NSBundle+Utils.swift
33
// SwiftMessages
44
//
5-
// Created by Tim Moose on 8/8/16.
6-
// Copyright © 2016 SwiftKick Mobile. All rights reserved.
5+
// Created by Timothy Moose on 8/8/16.
6+
// Copyright © 2016 SwiftKick Mobile LLC. All rights reserved.
77
//
88

99
import Foundation

‎SwiftMessages/PassthroughView.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// PassthroughView.swift
33
// SwiftMessages
44
//
5-
// Created by Tim Moose on 8/5/16.
6-
// Copyright © 2016 SwiftKick Mobile. All rights reserved.
5+
// Created by Timothy Moose on 8/5/16.
6+
// Copyright © 2016 SwiftKick Mobile LLC. All rights reserved.
77
//
88

99
import UIKit

‎SwiftMessages/PassthroughWindow.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// PassthroughWindow.swift
33
// SwiftMessages
44
//
5-
// Created by Tim Moose on 8/5/16.
6-
// Copyright © 2016 SwiftKick Mobile. All rights reserved.
5+
// Created by Timothy Moose on 8/5/16.
6+
// Copyright © 2016 SwiftKick Mobile LLC. All rights reserved.
77
//
88

99
import UIKit

‎SwiftMessages/Presenter.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// MessagePresenter.swift
33
// SwiftMessages
44
//
5-
// Created by Tim Moose on 7/30/16.
6-
// Copyright © 2016 SwiftKick Mobile. All rights reserved.
5+
// Created by Timothy Moose on 7/30/16.
6+
// Copyright © 2016 SwiftKick Mobile LLC. All rights reserved.
77
//
88

99
import UIKit

‎SwiftMessages/Resources/MessageView.xib

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@
1818
<view contentMode="scaleToFill" horizontalHuggingPriority="252" horizontalCompressionResistancePriority="752" misplaced="YES" translatesAutoresizingMaskIntoConstraints="NO" id="GEX-VM-06d" userLabel="Icon container">
1919
<rect key="frame" x="0.0" y="0.0" width="50" height="47"/>
2020
<subviews>
21-
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="252" verticalHuggingPriority="251" horizontalCompressionResistancePriority="752" text="😬" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="1Bu-DY-qZo" userLabel="Icon label">
21+
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="252" verticalHuggingPriority="251" horizontalCompressionResistancePriority="752" misplaced="YES" text="😬" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="1Bu-DY-qZo" userLabel="Icon label">
2222
<rect key="frame" x="0.0" y="0.0" width="38" height="46"/>
2323
<fontDescription key="fontDescription" type="system" pointSize="38"/>
2424
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
2525
<nil key="highlightedColor"/>
2626
</label>
27-
<imageView userInteractionEnabled="NO" contentMode="center" horizontalHuggingPriority="252" verticalHuggingPriority="251" horizontalCompressionResistancePriority="752" image="errorIcon" translatesAutoresizingMaskIntoConstraints="NO" id="PbA-QP-XpA" userLabel="Icon image view">
27+
<imageView userInteractionEnabled="NO" contentMode="center" horizontalHuggingPriority="252" verticalHuggingPriority="251" horizontalCompressionResistancePriority="752" misplaced="YES" image="errorIcon" translatesAutoresizingMaskIntoConstraints="NO" id="PbA-QP-XpA" userLabel="Icon image view">
2828
<rect key="frame" x="0.0" y="0.0" width="38" height="46"/>
2929
</imageView>
3030
</subviews>

‎SwiftMessages/SwiftMessages.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// SwiftMessages.h
33
// SwiftMessages
44
//
5-
// Created by Tim Moose on 8/9/16.
6-
// Copyright © 2016 SwiftKick Mobile. All rights reserved.
5+
// Created by Timothy Moose on 8/9/16.
6+
// Copyright © 2016 SwiftKick Mobile LLC. All rights reserved.
77
//
88

99
#import <UIKit/UIKit.h>

‎SwiftMessages/SwiftMessages.swift

+42-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// SwiftMessages.swift
33
// SwiftMessages
44
//
5-
// Created by Tim Moose on 8/1/16.
6-
// Copyright © 2016 SwiftKick Mobile. All rights reserved.
5+
// Created by Timothy Moose on 8/1/16.
6+
// Copyright © 2016 SwiftKick Mobile LLC. All rights reserved.
77
//
88

99
import UIKit
@@ -236,6 +236,46 @@ public class SwiftMessages: PresenterDelegate {
236236
}
237237
}
238238

239+
/*
240+
MARK: - Creating views
241+
*/
242+
243+
extension SwiftMessages {
244+
245+
public class func viewFromNib<T: UIView>(filesOwner: AnyObject = NSNull.init()) throws -> T {
246+
let name = T.description().componentsSeparatedByString(".").last
247+
assert(name != nil)
248+
let view: T = try viewFromNib(named: name!, bundle: nil, filesOwner: filesOwner)
249+
return view
250+
}
251+
252+
public class func viewFromNib<T: UIView>(named name: String, filesOwner: AnyObject = NSNull.init()) throws -> T {
253+
let view: T = try viewFromNib(named: name, bundle: nil, filesOwner: filesOwner)
254+
return view
255+
}
256+
257+
public class func viewFromNib<T: UIView>(named name: String, bundle: NSBundle, filesOwner: AnyObject = NSNull.init()) throws -> T {
258+
let view: T = try viewFromNib(named: name, bundle: bundle, filesOwner: filesOwner)
259+
return view
260+
}
261+
262+
private class func viewFromNib<T: UIView>(named name: String, bundle: NSBundle? = nil, filesOwner: AnyObject = NSNull.init()) throws -> T {
263+
let resolvedBundle: NSBundle
264+
if let bundle = bundle {
265+
resolvedBundle = bundle
266+
} else {
267+
if NSBundle.mainBundle().pathForResource(name, ofType: "nib") != nil {
268+
resolvedBundle = NSBundle.mainBundle()
269+
} else {
270+
resolvedBundle = NSBundle.frameworkBundle()
271+
}
272+
}
273+
let arrayOfViews = resolvedBundle.loadNibNamed(name, owner: filesOwner, options: nil)
274+
guard let view = arrayOfViews.flatMap( { $0 as? T} ).first else { throw Error.CannotLoadViewFromNib(nibName: name) }
275+
return view
276+
}
277+
}
278+
239279
/*
240280
MARK: - Static APIs
241281
*/

0 commit comments

Comments
 (0)