Created
December 22, 2021 15:44
-
-
Save azonov/5f4b2a1c0dfb49f6781136be3fd2906c to your computer and use it in GitHub Desktop.
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
import UIKit | |
public class ScrollableStackView: UIView { | |
// MARK: Private Properties | |
private lazy var scrollView: UIScrollView = { | |
let scrollView = UIScrollView(frame: .zero) | |
scrollView.backgroundColor = .clear | |
scrollView.translatesAutoresizingMaskIntoConstraints = false | |
scrollView.layoutMargins = .zero | |
return scrollView | |
}() | |
private lazy var stackView: UIStackView = { | |
let stackView = UIStackView(frame: .zero) | |
stackView.translatesAutoresizingMaskIntoConstraints = false | |
stackView.axis = .vertical | |
stackView.alignment = .fill | |
stackView.distribution = .fill | |
return stackView | |
}() | |
// MARK: Public Properties | |
var showsVerticalScrollIndicator: Bool { | |
get { | |
scrollView.showsVerticalScrollIndicator | |
} | |
set { | |
scrollView.showsVerticalScrollIndicator = newValue | |
} | |
} | |
var refreshControl: UIRefreshControl? { | |
get { | |
scrollView.refreshControl | |
} | |
set { | |
scrollView.refreshControl = newValue | |
} | |
} | |
// MARK: Life Cycle | |
override init(frame: CGRect) { | |
super.init(frame: frame) | |
setupView() | |
} | |
@available(*, unavailable) | |
required init?(coder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
// MARK: Public | |
func addView(_ view: UIView) { | |
stackView.addArrangedSubview(view) | |
} | |
func insertView(_ view: UIView, at index: Int) { | |
stackView.insertArrangedSubview(view, at: index) | |
} | |
func removeView(_ view: UIView) { | |
stackView.removeArrangedSubview(view) | |
} | |
// MARK: Private | |
private func setupView() { | |
translatesAutoresizingMaskIntoConstraints = false | |
clipsToBounds = true | |
addSubview(scrollView) | |
scrollView.addSubview(stackView) | |
NSLayoutConstraint.activate([ | |
scrollView.topAnchor.constraint(equalTo: topAnchor), | |
scrollView.leadingAnchor.constraint(equalTo: leadingAnchor), | |
scrollView.trailingAnchor.constraint(equalTo: trailingAnchor), | |
scrollView.bottomAnchor.constraint(equalTo: bottomAnchor), | |
stackView.topAnchor.constraint(equalTo: scrollView.topAnchor), | |
stackView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor), | |
stackView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor), | |
stackView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor), | |
stackView.widthAnchor.constraint(equalTo: scrollView.widthAnchor) | |
]) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment