Skip to content

Instantly share code, notes, and snippets.

@jayesh15111988
Created February 23, 2019 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jayesh15111988/950a1196a6731b79239049e0652af5d2 to your computer and use it in GitHub Desktop.
Save jayesh15111988/950a1196a6731b79239049e0652af5d2 to your computer and use it in GitHub Desktop.
//
// ScrollViewSupportViewController.swift
// Sample
//
// Created by Jayesh Kawli on 2/23/19.
// Copyright © 2019 Jayesh Kawli. All rights reserved.
//
import UIKit
class ScrollViewSupportViewController: UIViewController {
enum Constants {
static let horizontalPadding: CGFloat = 20.0
static let verticalPadding: CGFloat = 100.0
}
var autolayoutScrollView: ScrollViewAutolayoutCreator!
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .white
self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Top", style: .done, target: self, action: #selector(goToTop))
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Bottom", style: .done, target: self, action: #selector(goToBottom))
autolayoutScrollView = ScrollViewAutolayoutCreator(superView: view)
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.numberOfLines = 0
label.text = "hello hi buddy\nadadasd\nadadasdas d sd asd asd \na da d adas d\nasdasd"
label.backgroundColor = .green
let sampleView = UIView()
sampleView.translatesAutoresizingMaskIntoConstraints = false
sampleView.backgroundColor = .red
let bottomLabel = UILabel()
bottomLabel.translatesAutoresizingMaskIntoConstraints = false
bottomLabel.numberOfLines = 0
bottomLabel.text = "hello hi buddy\nadadasd\nadadasdas d sd asd asd \na da d adas d\nasdasd"
bottomLabel.backgroundColor = .yellow
let contentView = autolayoutScrollView.contentView
contentView.addSubview(label)
contentView.addSubview(sampleView)
contentView.addSubview(bottomLabel)
// Attach horizontal Constraints
autolayoutScrollView.addHorizontalConstraints(views: [label, sampleView, bottomLabel], horizontalPadding: Constants.horizontalPadding)
// MARK: Optional Vertical Constraints for positions. This can be done manually or through library as depicted in example below.
// NSLayoutConstraint.activate([
// label.topAnchor.constraint(equalTo: contentView.topAnchor, constant: Constants.padding),
// sampleView.topAnchor.constraint(equalTo: label.bottomAnchor, constant: 400.0),
// bottomLabel.topAnchor.constraint(equalTo: sampleView.bottomAnchor, constant: 400.0),
// bottomLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -Constants.padding)
// ])
autolayoutScrollView.addVerticalConstraints(views: [label, sampleView, bottomLabel], verticalPadding: Constants.verticalPadding)
// Vertical Constraints for height. Make sure these are set up correctly in the client code. Library doesn't have support to add height constraints for individual views
NSLayoutConstraint.activate([
sampleView.heightAnchor.constraint(equalToConstant: 200.0)
])
}
}
extension ScrollViewSupportViewController {
@objc func goToTop() {
autolayoutScrollView.scrollToTop()
}
@objc func goToBottom() {
autolayoutScrollView.scrollToBottom()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment