Last active
December 17, 2019 03:21
-
-
Save JAIRMG/350aefa65f16906c1a834964456ba4ec to your computer and use it in GitHub Desktop.
Scroll view with the first element
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 | |
class ScrollViewController: UIViewController { | |
let scrollView: UIScrollView = { | |
let scrollView = UIScrollView() | |
scrollView.translatesAutoresizingMaskIntoConstraints = false | |
scrollView.backgroundColor = .lightGray | |
return scrollView | |
}() | |
let containerView: UIView = { | |
let view = UIView() | |
view.translatesAutoresizingMaskIntoConstraints = false | |
view.backgroundColor = .white | |
return view | |
}() | |
let redButton: UIButton = { | |
let btn = UIButton() | |
btn.translatesAutoresizingMaskIntoConstraints = false | |
btn.backgroundColor = .red | |
return btn | |
}() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
view.backgroundColor = .white | |
addScrollView() | |
addContainerView() | |
addRedButton() | |
} | |
private func addScrollView() { | |
view.addSubview(scrollView) | |
scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true | |
scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true | |
scrollView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true | |
scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true | |
} | |
private func addContainerView() { | |
scrollView.addSubview(containerView) | |
containerView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true | |
containerView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true | |
containerView.topAnchor.constraint(equalTo: scrollView.topAnchor).isActive = true | |
containerView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor).isActive = true | |
} | |
private func addRedButton() { | |
containerView.addSubview(redButton) | |
redButton.topAnchor.constraint(equalTo: containerView.topAnchor).isActive = true | |
redButton.leadingAnchor.constraint(equalTo: containerView.leadingAnchor, constant: 10).isActive = true | |
redButton.trailingAnchor.constraint(equalTo: containerView.trailingAnchor, constant: -10).isActive = true | |
redButton.heightAnchor.constraint(equalToConstant: 200).isActive = true | |
// IMPORTANT 🚨 | |
scrollView.bottomAnchor.constraint(equalTo: redButton.bottomAnchor).isActive = true | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment