#23 UIScrollView layout programming/coding without storyboard

How to set contents that can make a UIScrollView’s scroll area autoresizing.

A UIScrollView consists of

  1. a content layout guide
  2. a frame layout guide

Frame layout is what size that UIScrollView show on the view.

Content layout is that scroll area really size.

  • Create UIScrollView
var testScrollView: UIScrollView = {var scrollView = UIScrollView()scrollView.translatesAutoresizingMaskIntoConstraints = falsereturn scrollView}()
  • set Constraint — this is frame layout guide
testScrollView.topAnchor.constraint(equalTo: view.topAnchor).isActive = truetestScrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = truetestScrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = truetestScrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
  • content layout guide
testScrollView.contentLayoutGuide.owningView?.topAnchor.constraint(equalTo: view.topAnchor).isActive = truetestScrollView.contentLayoutGuide.owningView?.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = truetestScrollView.contentLayoutGuide.owningView?.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = truetestScrollView.contentLayoutGuide.owningView?.bottomAnchor.constraint(equalTo: (The last component).bottomAnchor).isActive = true

if set by this, testScrollView’s scroll area will follow the last component’s bottomAnchor.

— — — — — — — — — — — — — — — — — — — — — — — —

If you want to use UIScrollView by storyboard.

And you are wondering how to set UIScrollView’s content.

You can watch the url down here.

If you want to set contents without storyboard.

I will talk about it now.

First, if you never use layout programming before.

You need to watch the article here.

Photo by AltumCode on Unsplash

--

--

--

Wish me luck on the way become iOS app developer!

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to fail miserably with RxSwift + MVVM

How to Build a Static/Dynamic Framework for iOS?

Beginner iOS Dev — In App Purchase (IAP) made simple with SwiftyStoreKit.

“Simple” SpriteKit-Like Touch Handling in Flutter (With Flame)

Setting Up Your Swift Playground App Project

Using Core Data in Your SwiftUI App with Combine, MVVM and Protocols

Using Charles & Mockoon in IOS

Crash when re-activate weak NSLayoutConstraint property

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
Stephen Huang

Stephen Huang

Wish me luck on the way become iOS app developer!

More from Medium

Refactor iOS ble auto reconnect logic: From 30% CPU usage to zero CPU usage

UICollectionView and UICollectionViewDiffableDataSource

Adding Statsig to iOS

Protecting ImageView content on iOS