Visual component realizes a horizontally scrollable list of cards.
CocoaPods is the preffered way to add HorizontalCardsView to your project.
Just add following line to your Podfile
pod 'HorizontalCardsView'
Then run a pod install
inside your terminal.
After that you can include HorizontalCardsView wherever you need it with
import HorizontalCardsView
- Add
UIView
to storyboard and setCustom Class
asHorizontalCardsView
.
- Set you
ViewController
to conformHorizontalCardsDataSource
andHorizontalCardsDelegate
protocols. - Setup params
viewsSource
anddelegate
inviewDidLoad()
for yourHorizontalCardsView
instance. Also, you should define paramscardSpasing
andinsets
. Height of cards is calculated as the height ofHorizontalCardsView
minus top and bottom insets. WIdth of cards is calculated as the width ofHorizontalCardsView
withcardWidthFactor
or width ofHorizontalCardsView
minus left and right insets if the component contains a single card.
class ViewController: UIViewController, HorizontalCardsDataSource, HorizontalCardsDelegate {
@IBOutlet weak var horizontalCardsView: HorizontalCardsView!
// ...
override func viewDidLoad() {
horizontalCardsView.dataSource = self
horizontalCardsView.delegate = self
horizontalCardsView.cellSpacing = 15
horizontalCardsView.inset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
}
// ...
func horizontalCardsViewNumberOfItems(_ collectionView: HorizontalCardsView) -> Int {
return 5
}
func horizontalCardsView(_: HorizontalCardsView, viewForIndex index: Int) -> HorizontalCardView {
let bundle = Bundle(for: SampleView.self)
let view = bundle.loadNibNamed("SampleView", owner: self, options: nil)?.first
return view
}
func horizontalCardsView(_: HorizontalCardsView, didSelectItemAtIndex index: Int) {
print("A view with index \(index) was selected.")
}
// ...
}
- Views which you want to display in the scrollable list must be descendants of
HorizontalCardView
class. OverrideprepareForReuse()
method, if you want to do some actions before views will be reused.