As a follow up to my previous story on how to apply auto-layout to UIScrollViews in interface builder, I want to share my handy protocol that will let you easily create a scrollable view controller in code.

import UIKitprotocol RootViewScrollable: UIViewController {
var scrollView: UIScrollView { get set }
var contentView: UIView { get set }
func setupScrollView()
}
extension RootViewScrollable {
func setupScrollView() {
view.addSubview(scrollView)

scrollView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate(
[
scrollView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
scrollView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
scrollView.topAnchor.constraint(equalTo: view.topAnchor),
scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
]
)
scrollView.addSubview(contentView) let contentGuide = scrollView.contentLayoutGuide let frameGuide = scrollView.frameLayoutGuide…

Are you a college student and you were impressed by the powers of a coder in a sci-tech movie? Or are you a person who is curious about the cryptic language people use to teach computers to do their work? Or are you one of those retired folks who want to learn programming instead of learning to play a grand piano? Whoever you are my friend, if you want to learn programming and have never had anyone to tell you about it, you’ve found the person you have been looking for!

In this ten days series, I am going to…


I always forget how to properly add a UIScrollView to the root view of a view controller in Interface Builder. Since, I have a poor memory, this article is a note for myself and other people who want to remind themselves how to do it.

Steps

Note: The names of objects in italics are the same ones as those used in the picture below for easy reference.

  1. Add a UIScrollView (Scroll View) to the Root View.
  2. Align trailing and leading constraints to the Safe Area.
  3. Align Scroll View’s bottom and top to Root View.
  4. Add a UIView (we’ll call it the…

Furqan Khan

Software developer, traveler, history-philosophy-science enthusiast.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store