Skip to content

Commit

Permalink
Added ability to create a scroll view from any view (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
MaherKSantina authored Aug 15, 2018
1 parent 52fc6a4 commit 7920a14
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
42 changes: 42 additions & 0 deletions MSScrollView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// MSScrollView.swift
// MSAutoView
//
// Created by Maher Santina on 8/14/18.
//

import UIKit

open class MSScrollView<T: UIView>: UIScrollView {
public var mainView: T?

func initView(mainView: T) {
self.mainView?.removeFromSuperview()
addSubviewWithConstraints(mainView)
let constraint = NSLayoutConstraint(item: mainView, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0)
addConstraint(constraint)
self.mainView = mainView
}

}

public protocol ScrollViewContainable {
associatedtype ScrollViewContainedType: UIView

var scrollView: MSScrollView<ScrollViewContainedType> { get }
static var scrollView: MSScrollView<ScrollViewContainedType>.Type { get }
}

extension ScrollViewContainable where Self: UIView {
public var scrollView: MSScrollView<Self> {
let scrollView = MSScrollView<Self>()
scrollView.initView(mainView: self)
return scrollView
}

public static var scrollView: MSScrollView<Self>.Type {
return MSScrollView<Self>.self
}
}

extension UIView: ScrollViewContainable { }
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,14 @@ This class is an `open` class so you can subclass it as you wish to add more fea
### Creating a collection view cell from any view
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`

### Creating a scroll view from any view
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
```swift
let tallView = TallView()
tallView.anyLabel.text = "This is a dummy text"
view.addSubviewWithConstraints(tallView.scrollView)
```

### Using a default value for all instances of the view
You can do this in 2 ways:
1. Set the value in code:
Expand Down

0 comments on commit 7920a14

Please sign in to comment.