Skip to content

Commit 6d079d9

Browse files
committed
Add NibLoader
1 parent 6d33b14 commit 6d079d9

File tree

5 files changed

+124
-0
lines changed

5 files changed

+124
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//
2+
// NibStackCell.swift
3+
// StackScrollView
4+
//
5+
// Created by muukii on 5/17/17.
6+
// Copyright © 2017 muukii. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
final class NibStackCell: UIView {
12+
13+
@IBOutlet weak var slider: UISlider!
14+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="12120" systemVersion="16E195" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES">
3+
<device id="retina4_7" orientation="portrait">
4+
<adaptation id="fullscreen"/>
5+
</device>
6+
<dependencies>
7+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="12088"/>
8+
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
9+
</dependencies>
10+
<objects>
11+
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/>
12+
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
13+
<view contentMode="scaleToFill" id="iN0-l3-epB" customClass="NibStackCell" customModule="StackScrollView_Demo" customModuleProvider="target">
14+
<rect key="frame" x="0.0" y="0.0" width="375" height="92"/>
15+
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
16+
<subviews>
17+
<slider opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" value="0.5" minValue="0.0" maxValue="1" translatesAutoresizingMaskIntoConstraints="NO" id="Oxa-dM-rc7">
18+
<rect key="frame" x="14" y="32" width="347" height="29"/>
19+
</slider>
20+
</subviews>
21+
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
22+
<constraints>
23+
<constraint firstAttribute="trailing" secondItem="Oxa-dM-rc7" secondAttribute="trailing" constant="16" id="7Xb-bT-Xpk"/>
24+
<constraint firstAttribute="bottom" secondItem="Oxa-dM-rc7" secondAttribute="bottom" constant="32" id="FBw-xm-eK3"/>
25+
<constraint firstItem="Oxa-dM-rc7" firstAttribute="leading" secondItem="iN0-l3-epB" secondAttribute="leading" constant="16" id="f5J-Cs-PHd"/>
26+
<constraint firstItem="Oxa-dM-rc7" firstAttribute="top" secondItem="iN0-l3-epB" secondAttribute="top" constant="32" id="u1z-5l-gkR"/>
27+
</constraints>
28+
<nil key="simulatedStatusBarMetrics"/>
29+
<nil key="simulatedTopBarMetrics"/>
30+
<nil key="simulatedBottomBarMetrics"/>
31+
<freeformSimulatedSizeMetrics key="simulatedDestinationMetrics"/>
32+
<connections>
33+
<outlet property="slider" destination="Oxa-dM-rc7" id="yCB-2c-yPU"/>
34+
</connections>
35+
<point key="canvasLocation" x="32" y="153"/>
36+
</view>
37+
</objects>
38+
</document>

StackScrollView-Demo/ViewController.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,19 @@ class ViewController: UIViewController {
120120

121121
views.append(fullSeparator())
122122

123+
views.append(MarginStackCell(height: 40, backgroundColor: marginColor))
124+
125+
do {
126+
// Load from XIB
127+
128+
let cell = NibLoader<NibStackCell>().load()
129+
130+
views.append(fullSeparator())
131+
views.append(cell)
132+
views.append(fullSeparator())
133+
134+
}
135+
123136
views.append(MarginStackCell(height: 40, backgroundColor: marginColor))
124137

125138
stackScrollView.append(views: views)

StackScrollView.xcodeproj/project.pbxproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
4B1B51BA1ECB85C70034A3D9 /* NibLoader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B1B51B91ECB85C70034A3D9 /* NibLoader.swift */; };
11+
4B1B51BC1ECB88230034A3D9 /* NibStackCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = 4B1B51BB1ECB88220034A3D9 /* NibStackCell.xib */; };
12+
4B1B51BE1ECB882D0034A3D9 /* NibStackCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B1B51BD1ECB882D0034A3D9 /* NibStackCell.swift */; };
1013
4B6C2D9E1D8DA3FB003D3A46 /* StackScrollView.h in Headers */ = {isa = PBXBuildFile; fileRef = 4B6C2D9C1D8DA3FB003D3A46 /* StackScrollView.h */; settings = {ATTRIBUTES = (Public, ); }; };
1114
4B6C2DA61D8DA42A003D3A46 /* StackScrollView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B6C2DA41D8DA42A003D3A46 /* StackScrollView.swift */; };
1215
4B6C2DA71D8DA42A003D3A46 /* StackCellType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B6C2DA51D8DA42A003D3A46 /* StackCellType.swift */; };
@@ -58,6 +61,9 @@
5861

5962
/* Begin PBXFileReference section */
6063
107B190065E8096D8802EE36 /* Pods-StackScrollView-Demo.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-StackScrollView-Demo.release.xcconfig"; path = "Pods/Target Support Files/Pods-StackScrollView-Demo/Pods-StackScrollView-Demo.release.xcconfig"; sourceTree = "<group>"; };
64+
4B1B51B91ECB85C70034A3D9 /* NibLoader.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NibLoader.swift; sourceTree = "<group>"; };
65+
4B1B51BB1ECB88220034A3D9 /* NibStackCell.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = NibStackCell.xib; sourceTree = "<group>"; };
66+
4B1B51BD1ECB882D0034A3D9 /* NibStackCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NibStackCell.swift; sourceTree = "<group>"; };
6167
4B6C2D991D8DA3FB003D3A46 /* StackScrollView.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = StackScrollView.framework; sourceTree = BUILT_PRODUCTS_DIR; };
6268
4B6C2D9C1D8DA3FB003D3A46 /* StackScrollView.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = StackScrollView.h; sourceTree = "<group>"; };
6369
4B6C2D9D1D8DA3FB003D3A46 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
@@ -134,6 +140,7 @@
134140
4B6C2DA41D8DA42A003D3A46 /* StackScrollView.swift */,
135141
4B6C2DA51D8DA42A003D3A46 /* StackCellType.swift */,
136142
4B83ACE21EB8756800F92569 /* StackCell.swift */,
143+
4B1B51B91ECB85C70034A3D9 /* NibLoader.swift */,
137144
4B6C2D9C1D8DA3FB003D3A46 /* StackScrollView.h */,
138145
4B6C2D9D1D8DA3FB003D3A46 /* Info.plist */,
139146
);
@@ -170,6 +177,8 @@
170177
4B83ACD61EB7C0C500F92569 /* ButtonStackCell.swift */,
171178
4B83ACD81EB7C0CB00F92569 /* MarginStackCell.swift */,
172179
4B83ACDC1EB8578400F92569 /* HeaderStackCell.swift */,
180+
4B1B51BD1ECB882D0034A3D9 /* NibStackCell.swift */,
181+
4B1B51BB1ECB88220034A3D9 /* NibStackCell.xib */,
173182
);
174183
path = "StackScrollView-Demo";
175184
sourceTree = "<group>";
@@ -292,6 +301,7 @@
292301
isa = PBXResourcesBuildPhase;
293302
buildActionMask = 2147483647;
294303
files = (
304+
4B1B51BC1ECB88230034A3D9 /* NibStackCell.xib in Resources */,
295305
4B6C2DBC1D8DA68E003D3A46 /* LaunchScreen.storyboard in Resources */,
296306
4B6C2DB91D8DA68E003D3A46 /* Assets.xcassets in Resources */,
297307
4B6C2DB71D8DA68E003D3A46 /* Main.storyboard in Resources */,
@@ -355,6 +365,7 @@
355365
files = (
356366
4B83ACE31EB8756800F92569 /* StackCell.swift in Sources */,
357367
4B6C2DA71D8DA42A003D3A46 /* StackCellType.swift in Sources */,
368+
4B1B51BA1ECB85C70034A3D9 /* NibLoader.swift in Sources */,
358369
4B6C2DA61D8DA42A003D3A46 /* StackScrollView.swift in Sources */,
359370
);
360371
runOnlyForDeploymentPostprocessing = 0;
@@ -375,6 +386,7 @@
375386
4B83ACD91EB7C0CB00F92569 /* MarginStackCell.swift in Sources */,
376387
4B83ACCD1EB7C00700F92569 /* DatePickerStackCell.swift in Sources */,
377388
4B83ACE11EB85EB200F92569 /* TapStackCell.swift in Sources */,
389+
4B1B51BE1ECB882D0034A3D9 /* NibStackCell.swift in Sources */,
378390
4B83ACDD1EB8578400F92569 /* HeaderStackCell.swift in Sources */,
379391
);
380392
runOnlyForDeploymentPostprocessing = 0;

StackScrollView/NibLoader.swift

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//
2+
// NibLoader.swift
3+
//
4+
// Copyright (c) 2017 muukii
5+
//
6+
// Permission is hereby granted, free of charge, to any person obtaining a copy
7+
// of this software and associated documentation files (the "Software"), to deal
8+
// in the Software without restriction, including without limitation the rights
9+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
// copies of the Software, and to permit persons to whom the Software is
11+
// furnished to do so, subject to the following conditions:
12+
//
13+
// The above copyright notice and this permission notice shall be included in
14+
// all copies or substantial portions of the Software.
15+
//
16+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
// THE SOFTWARE.
23+
24+
import UIKit
25+
26+
public struct NibLoader<T: UIView> {
27+
28+
public let name: String?
29+
public let bundle: Bundle?
30+
31+
public init(name: String? = nil, bundle: Bundle? = nil) {
32+
self.name = name
33+
self.bundle = bundle
34+
}
35+
36+
func nib() -> UINib {
37+
return UINib(nibName: name ?? String(describing: T.self), bundle: bundle ?? Bundle(for: T.self))
38+
}
39+
40+
public func load() -> T {
41+
let nib = self.nib()
42+
guard let view = nib.instantiate(withOwner: nil, options: nil).first as? T else {
43+
fatalError("The nib \(nib) expected its root view to be of type \(T.self)")
44+
}
45+
return view
46+
}
47+
}

0 commit comments

Comments
 (0)