diff --git a/MSScrollView.swift b/MSScrollView.swift new file mode 100644 index 0000000..8691174 --- /dev/null +++ b/MSScrollView.swift @@ -0,0 +1,42 @@ +// +// MSScrollView.swift +// MSAutoView +// +// Created by Maher Santina on 8/14/18. +// + +import UIKit + +open class MSScrollView: 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 { get } + static var scrollView: MSScrollView.Type { get } +} + +extension ScrollViewContainable where Self: UIView { + public var scrollView: MSScrollView { + let scrollView = MSScrollView() + scrollView.initView(mainView: self) + return scrollView + } + + public static var scrollView: MSScrollView.Type { + return MSScrollView.self + } +} + +extension UIView: ScrollViewContainable { } diff --git a/README.md b/README.md index dbf3735..5f9fee2 100644 --- a/README.md +++ b/README.md @@ -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: