Skip to content

Commit 7920a14

Browse files
Added ability to create a scroll view from any view (#12)
1 parent 52fc6a4 commit 7920a14

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

MSScrollView.swift

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//
2+
// MSScrollView.swift
3+
// MSAutoView
4+
//
5+
// Created by Maher Santina on 8/14/18.
6+
//
7+
8+
import UIKit
9+
10+
open class MSScrollView<T: UIView>: UIScrollView {
11+
public var mainView: T?
12+
13+
func initView(mainView: T) {
14+
self.mainView?.removeFromSuperview()
15+
addSubviewWithConstraints(mainView)
16+
let constraint = NSLayoutConstraint(item: mainView, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0)
17+
addConstraint(constraint)
18+
self.mainView = mainView
19+
}
20+
21+
}
22+
23+
public protocol ScrollViewContainable {
24+
associatedtype ScrollViewContainedType: UIView
25+
26+
var scrollView: MSScrollView<ScrollViewContainedType> { get }
27+
static var scrollView: MSScrollView<ScrollViewContainedType>.Type { get }
28+
}
29+
30+
extension ScrollViewContainable where Self: UIView {
31+
public var scrollView: MSScrollView<Self> {
32+
let scrollView = MSScrollView<Self>()
33+
scrollView.initView(mainView: self)
34+
return scrollView
35+
}
36+
37+
public static var scrollView: MSScrollView<Self>.Type {
38+
return MSScrollView<Self>.self
39+
}
40+
}
41+
42+
extension UIView: ScrollViewContainable { }

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,14 @@ This class is an `open` class so you can subclass it as you wish to add more fea
185185
### Creating a collection view cell from any view
186186
Creating a collection view cell from any view acts similar as creating a table view cell. But, you would use the extension variable `collectionViewCell` instead of the `tableViewCell`
187187

188+
### Creating a scroll view from any view
189+
Creating a scroll view is the same as creating a table view/collection view. Assuming that you have a tall view of class `TallView` which has a label called `anyLabel`, you can do the following in your view controller
190+
```swift
191+
let tallView = TallView()
192+
tallView.anyLabel.text = "This is a dummy text"
193+
view.addSubviewWithConstraints(tallView.scrollView)
194+
```
195+
188196
### Using a default value for all instances of the view
189197
You can do this in 2 ways:
190198
1. Set the value in code:

0 commit comments

Comments
 (0)