Skip to content

Instantly share code, notes, and snippets.

@jayesh15111988
Created April 22, 2023 10:55
Show Gist options
  • Save jayesh15111988/2f118feb416943650cb98328ed86d284 to your computer and use it in GitHub Desktop.
Save jayesh15111988/2f118feb416943650cb98328ed86d284 to your computer and use it in GitHub Desktop.
A source code for horizontally scrolling content view
//
// HorizontalScrollView.swift
// Test
//
// Created by Jayesh Kawli on 4/21/23.
//
import UIKit
class HorizontalScrollView : UIViewController {
private let stackView: UIStackView = {
let stackView = UIStackView()
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.axis = .horizontal
stackView.spacing = 20
return stackView
}()
private let scrollView: UIScrollView = {
let scrollView = UIScrollView()
scrollView.translatesAutoresizingMaskIntoConstraints = false
scrollView.showsHorizontalScrollIndicator = false
return scrollView
}()
override func viewDidLoad() {
super.viewDidLoad()
setupViews()
}
private func setupViews() {
view.backgroundColor = .white
view.addSubview(scrollView)
scrollView.addSubview(stackView)
scrollView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
scrollView.heightAnchor.constraint(equalToConstant: 50).isActive = true
stackView.topAnchor.constraint(equalTo: scrollView.topAnchor).isActive = true
stackView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor).isActive = true
stackView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor).isActive = true
stackView.heightAnchor.constraint(equalTo: scrollView.heightAnchor).isActive = true
for _ in 0..<20 {
let square = UIView()
square.translatesAutoresizingMaskIntoConstraints = true
square.widthAnchor.constraint(equalToConstant: 50).isActive = true
square.heightAnchor.constraint(equalToConstant: 50).isActive = true
square.backgroundColor = .blue
stackView.addArrangedSubview(square)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment