Created
May 18, 2018 15:22
-
-
Save iamchiwon/76b5d7e8c5a19f7ddb0cbf02d7449a34 to your computer and use it in GitHub Desktop.
creating view programatically
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@discardableResult | |
func createView<T>(_ view: T, | |
parent: UIView?, | |
setting: ((T) -> ())? = nil, | |
constraint: ((ConstraintMaker) -> ())? = nil) -> T where T: UIView { | |
switch parent { | |
case let stack as UIStackView: | |
stack.addArrangedSubview(view) | |
case let collectionCell as UICollectionViewCell: | |
collectionCell.contentView.addSubview(view) | |
case let tableCell as UITableViewCell: | |
tableCell.contentView.addSubview(view) | |
default: | |
parent?.addSubview(view) | |
} | |
if let setting = setting { | |
setting(view) | |
} | |
if let constraint = constraint { | |
view.snp.makeConstraints(constraint) | |
} | |
return view | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
import SnapKit
import UIKit