Skip to content

Commit 92a45db

Browse files
authored
Merge pull request #6 from redbooth/controllerToViewRefactor
Controller to view refactor
2 parents db158ff + 1fdeeae commit 92a45db

File tree

8 files changed

+144
-108
lines changed

8 files changed

+144
-108
lines changed

MyPlayground.playground/Contents.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,8 @@ let pagedView = SimplePagedView(
1717
with: green, red, orange
1818
)
1919

20+
pagedView.frame = CGRect(x: 0, y: 0, width: 350, height: 700)
21+
22+
pagedView.pageIndicatorIsInteractive = true
23+
2024
PlaygroundPage.current.liveView = pagedView

README.md

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ pod 'SimplePagedView'
1212

1313
## Usage
1414

15+
### Programmatic setup
1516
```swift
1617
// Create a PagedViewController by providing it with a view for each page you'd like it to contain
17-
let pagedViewController = PagedViewController(with:
18+
let simplePagedView = SimplePagedView(with:
1819
LogoView(presenter: welcomePresenter),
1920
CardPageView(image: ThemeManager.Images.welcomeTourSlide1,
2021
subtitle: "Complete and resolve tasks on the go"),
@@ -24,14 +25,5 @@ let pagedViewController = PagedViewController(with:
2425
subtitle: "Add files and pictures in seconds")
2526
)
2627

27-
// Add the pagedViewController as a child view controller
28-
self.add(pagedViewController) { (childView) -> [NSLayoutConstraint] in
29-
// Return an array of constraints to apply to the paged view
30-
return [
31-
childView.topAnchor.constraint(equalTo: view.readableContentGuide.topAnchor),
32-
childView.leadingAnchor.constraint(equalTo: view.readableContentGuide.leadingAnchor),
33-
childView.trailingAnchor.constraint(equalTo: view.readableContentGuide.trailingAnchor),
34-
childView.bottomAnchor.constraint(equalTo: view.readableContentGuide.bottomAnchor)
35-
]
36-
}
28+
// Add as subview and setup constraints/frame
3729
```

SimplePagedViewFramework.xcodeproj/project.pbxproj

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

99
/* Begin PBXBuildFile section */
10+
B206EB0D21D69F1E00F39BD3 /* UIView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B206EB0C21D69F1E00F39BD3 /* UIView.swift */; };
1011
B2C6870F21C191F2000031AB /* SimplePagedViewFramework.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B2C6870521C191F2000031AB /* SimplePagedViewFramework.framework */; };
1112
B2C6871421C191F2000031AB /* SimplePagedViewFrameworkTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B2C6871321C191F2000031AB /* SimplePagedViewFrameworkTests.swift */; };
1213
B2C6872821C19218000031AB /* NSLayoutConstraint.swift in Sources */ = {isa = PBXBuildFile; fileRef = B2C6872021C19217000031AB /* NSLayoutConstraint.swift */; };
1314
B2C6872921C19218000031AB /* UIImage.swift in Sources */ = {isa = PBXBuildFile; fileRef = B2C6872121C19217000031AB /* UIImage.swift */; };
14-
B2C6872B21C19218000031AB /* ExternallyInteractiveUIView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B2C6872421C19217000031AB /* ExternallyInteractiveUIView.swift */; };
1515
B2C6872C21C19218000031AB /* SimplePagedView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B2C6872521C19218000031AB /* SimplePagedView.swift */; };
1616
B2C6873121C1AE10000031AB /* UIViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B2C6873021C1AE10000031AB /* UIViewController.swift */; };
1717
B2C6898421C342D5000031AB /* PageDotsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B2C6898321C342D5000031AB /* PageDotsView.swift */; };
@@ -37,14 +37,14 @@
3737
/* End PBXContainerItemProxy section */
3838

3939
/* Begin PBXFileReference section */
40+
B206EB0C21D69F1E00F39BD3 /* UIView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIView.swift; sourceTree = "<group>"; };
4041
B2C6870521C191F2000031AB /* SimplePagedViewFramework.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SimplePagedViewFramework.framework; sourceTree = BUILT_PRODUCTS_DIR; };
4142
B2C6870921C191F2000031AB /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
4243
B2C6870E21C191F2000031AB /* SimplePagedViewFrameworkTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = SimplePagedViewFrameworkTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
4344
B2C6871321C191F2000031AB /* SimplePagedViewFrameworkTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SimplePagedViewFrameworkTests.swift; sourceTree = "<group>"; };
4445
B2C6871521C191F2000031AB /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
4546
B2C6872021C19217000031AB /* NSLayoutConstraint.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSLayoutConstraint.swift; sourceTree = "<group>"; };
4647
B2C6872121C19217000031AB /* UIImage.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIImage.swift; sourceTree = "<group>"; };
47-
B2C6872421C19217000031AB /* ExternallyInteractiveUIView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ExternallyInteractiveUIView.swift; sourceTree = "<group>"; };
4848
B2C6872521C19218000031AB /* SimplePagedView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SimplePagedView.swift; sourceTree = "<group>"; };
4949
B2C6873021C1AE10000031AB /* UIViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIViewController.swift; sourceTree = "<group>"; };
5050
B2C6898321C342D5000031AB /* PageDotsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PageDotsView.swift; sourceTree = "<group>"; };
@@ -117,25 +117,17 @@
117117
B2C6872021C19217000031AB /* NSLayoutConstraint.swift */,
118118
B2C6872121C19217000031AB /* UIImage.swift */,
119119
B2C6873021C1AE10000031AB /* UIViewController.swift */,
120+
B206EB0C21D69F1E00F39BD3 /* UIView.swift */,
120121
);
121122
path = Extensions;
122123
sourceTree = "<group>";
123124
};
124-
B2C6872321C19217000031AB /* Helpers */ = {
125-
isa = PBXGroup;
126-
children = (
127-
B2C6872421C19217000031AB /* ExternallyInteractiveUIView.swift */,
128-
);
129-
path = Helpers;
130-
sourceTree = "<group>";
131-
};
132125
B2C6872F21C1A8C8000031AB /* Source */ = {
133126
isa = PBXGroup;
134127
children = (
135128
B2C6898521C9805B000031AB /* Errors */,
136129
B2C6872521C19218000031AB /* SimplePagedView.swift */,
137130
B2C6898221C342B6000031AB /* Views */,
138-
B2C6872321C19217000031AB /* Helpers */,
139131
B2C6871F21C19217000031AB /* Extensions */,
140132
);
141133
path = Source;
@@ -298,11 +290,11 @@
298290
isa = PBXSourcesBuildPhase;
299291
buildActionMask = 2147483647;
300292
files = (
293+
B206EB0D21D69F1E00F39BD3 /* UIView.swift in Sources */,
301294
B2C6898721C9806F000031AB /* DotsError.swift in Sources */,
302295
B2C6872821C19218000031AB /* NSLayoutConstraint.swift in Sources */,
303296
B2C689B221CA12E0000031AB /* DotView.swift in Sources */,
304297
B2C6898421C342D5000031AB /* PageDotsView.swift in Sources */,
305-
B2C6872B21C19218000031AB /* ExternallyInteractiveUIView.swift in Sources */,
306298
B2C6872C21C19218000031AB /* SimplePagedView.swift in Sources */,
307299
B2C6872921C19218000031AB /* UIImage.swift in Sources */,
308300
B2C6873121C1AE10000031AB /* UIViewController.swift in Sources */,

SimplePagedViewFramework/Example/SimplePagedViewExample/SimplePagedViewExample/ViewController.swift

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,16 @@ class ViewController: UIViewController {
3131
with: green, red, orange
3232
)
3333
pagedView.pageIndicatorIsInteractive = true
34+
pagedView.translatesAutoresizingMaskIntoConstraints = false
3435

35-
self.add(pagedView) { (child) -> [NSLayoutConstraint] in
36-
return [
37-
child.topAnchor.constraint(equalTo: view.topAnchor),
38-
child.bottomAnchor.constraint(equalTo: view.bottomAnchor),
39-
child.leadingAnchor.constraint(equalTo: view.leadingAnchor),
40-
child.trailingAnchor.constraint(equalTo: view.trailingAnchor)
41-
]
42-
}
36+
self.view.addSubview(pagedView)
37+
38+
NSLayoutConstraint.activate([
39+
pagedView.topAnchor.constraint(equalTo: view.topAnchor),
40+
pagedView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
41+
pagedView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
42+
pagedView.trailingAnchor.constraint(equalTo: view.trailingAnchor)
43+
])
4344
}
4445
}
4546

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import UIKit
2+
3+
extension UIView {
4+
public func replace<T: UIView>(
5+
subview: UIView,
6+
with other: UIView,
7+
constraints: (_ child: UIView, _ parent: T) -> [NSLayoutConstraint]
8+
) {
9+
let newConstraints = constraints(other, self as! T)
10+
guard let subviewIndex = subview.superview?.subviews.firstIndex(of: subview) else { fatalError() }
11+
subview.removeFromSuperview()
12+
self.insertSubview(other, at: subviewIndex)
13+
14+
NSLayoutConstraint.activate(newConstraints)
15+
}
16+
}

SimplePagedViewFramework/Source/Helpers/ExternallyInteractiveUIView.swift

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)