Skip to content

Instantly share code, notes, and snippets.

@leolelego
Created June 22, 2020 16:16
Show Gist options
  • Save leolelego/da76811aea6928e20f3b3ded7d99f8d2 to your computer and use it in GitHub Desktop.
Save leolelego/da76811aea6928e20f3b3ded7d99f8d2 to your computer and use it in GitHub Desktop.
StackScrollView for Swift
import UIKit
@available(iOS 9.0, *)
public class StackScrollView: UIView {
// MARK: - Properties
public let scrollView = UIScrollView()
public let stackView = UIStackView()
lazy var constraintLayoutHeight : NSLayoutConstraint = {
return self.stackView.heightAnchor.constraint(equalTo: self.scrollView.heightAnchor)
}()
lazy var constraintLayoutWidth : NSLayoutConstraint = {
return self.stackView.widthAnchor.constraint(equalTo: self.scrollView.widthAnchor)
}()
// MARK: - Init
override init(frame: CGRect) {
super.init(frame: frame)
ui_setup()
}
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
ui_setup()
}
// MARK: - Notifications
override public func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
// handle axis change in stackview
if keyPath == "axis"{
updateAxisConstraint()
}
}
// MARK: - Deinit
deinit{
stackView.removeObserver(self, forKeyPath: "axis")
}
}
@available(iOS 9.0, *)
extension LDStackScrollView {
// MARK: - setup the UI
func ui_setup(){
// ScrollView Setup
scrollView.translatesAutoresizingMaskIntoConstraints = false
addSubview(scrollView)
scrollView.topAnchor.constraint(equalTo: topAnchor).isActive = true
scrollView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
scrollView.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
scrollView.rightAnchor.constraint(equalTo: rightAnchor).isActive = true
scrollView.bounces = false
scrollView.showsVerticalScrollIndicator = false
scrollView.showsHorizontalScrollIndicator = false
// Stackview Setup
stackView.translatesAutoresizingMaskIntoConstraints = false
scrollView.addSubview(stackView)
stackView.topAnchor.constraint(equalTo: scrollView.topAnchor).isActive = true
stackView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor).isActive = true
stackView.leftAnchor.constraint(equalTo: scrollView.leftAnchor).isActive = true
stackView.rightAnchor.constraint(equalTo: scrollView.rightAnchor).isActive = true
stackView.axis = .vertical
stackView.alignment = .fill
stackView.distribution = .equalSpacing
updateAxisConstraint()
// Add Observer for stackview axis change handle
stackView.addObserver(self, forKeyPath: "axis", options: NSKeyValueObservingOptions.new, context: nil)
}
// MARK: - update constraint for axis change
func updateAxisConstraint(){
constraintLayoutWidth.isActive = stackView.axis == .vertical
constraintLayoutHeight.isActive = stackView.axis == .horizontal
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment