Created
April 22, 2023 10:55
-
-
Save jayesh15111988/2f118feb416943650cb98328ed86d284 to your computer and use it in GitHub Desktop.
A source code for horizontally scrolling content view
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
// | |
// 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