Skip to content

Instantly share code, notes, and snippets.

@DonMag
Created March 4, 2023 16:08
Show Gist options
  • Save DonMag/ec91e8a1fff4176bae316ba7035c7d30 to your computer and use it in GitHub Desktop.
Save DonMag/ec91e8a1fff4176bae316ba7035c7d30 to your computer and use it in GitHub Desktop.
Testing .contentSize in UIScrollView
//
// Created by Don Mag on 3/4/23.
//
import UIKit
class ContentSizeViewController: UIViewController, UIScrollViewDelegate {
let scrollView = UIScrollView()
let infoLabelA = UILabel()
let infoLabelB = UILabel()
let infoLabelC = UILabel()
var infoCount: Int = 0
let w: CGFloat = 800.0
let h: CGFloat = 1000.0
var fullSize: Bool = true {
didSet {
let newW = fullSize ? w : w * 0.5
let newH = fullSize ? h : h * 0.5
let newSZ = CGSize(width: newW, height: newH)
scrollView.contentSize = newSZ
infoLabelB.text = ".contentSize set to: \(newSZ)"
// so infoLabelA gets updated
updateInfo()
// so infoLabelC gets updated
scrollViewDidScroll(scrollView)
}
}
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor(white: 0.95, alpha: 1.0)
let g = view.safeAreaLayoutGuide
var config = UIButton.Configuration.filled()
config.buttonSize = .medium
config.cornerStyle = .medium
config.title = "Get .contentSize"
let btnA = UIButton(configuration: config)
btnA.addAction (
UIAction { _ in
self.updateInfo()
}, for: .touchUpInside
)
infoLabelA.backgroundColor = .cyan
infoLabelA.textAlignment = .center
infoLabelA.font = .systemFont(ofSize: 14, weight: .regular)
config.title = "Toggle .contentSize"
let btnB = UIButton(configuration: config)
btnB.addAction (
UIAction { _ in
self.fullSize.toggle()
}, for: .touchUpInside
)
infoLabelB.backgroundColor = .cyan
infoLabelB.textAlignment = .center
infoLabelB.font = .systemFont(ofSize: 14, weight: .regular)
// so we can see the scrollView frame
scrollView.backgroundColor = .systemRed
scrollView.layer.borderColor = UIColor.black.cgColor
scrollView.layer.borderWidth = 1
infoLabelC.backgroundColor = .cyan
infoLabelC.textAlignment = .center
infoLabelC.numberOfLines = 0
infoLabelC.font = .systemFont(ofSize: 14, weight: .regular)
[btnA, infoLabelA, btnB, infoLabelB, scrollView, infoLabelC].forEach { v in
v.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(v)
}
NSLayoutConstraint.activate([
btnA.topAnchor.constraint(equalTo: g.topAnchor, constant: 8.0),
btnA.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 40.0),
btnA.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -40.0),
infoLabelA.topAnchor.constraint(equalTo: btnA.bottomAnchor, constant: 8.0),
infoLabelA.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 40.0),
infoLabelA.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -40.0),
infoLabelA.heightAnchor.constraint(equalToConstant: 30.0),
btnB.topAnchor.constraint(equalTo: infoLabelA.bottomAnchor, constant: 8.0),
btnB.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 40.0),
btnB.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -40.0),
infoLabelB.topAnchor.constraint(equalTo: btnB.bottomAnchor, constant: 8.0),
infoLabelB.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 40.0),
infoLabelB.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -40.0),
infoLabelB.heightAnchor.constraint(equalToConstant: 30.0),
scrollView.topAnchor.constraint(equalTo: infoLabelB.bottomAnchor, constant: 20.0),
scrollView.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 60.0),
scrollView.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -60.0),
scrollView.bottomAnchor.constraint(equalTo: infoLabelC.topAnchor, constant: -20.0),
infoLabelC.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 40.0),
infoLabelC.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -40.0),
infoLabelC.heightAnchor.constraint(equalToConstant: 50.0),
infoLabelC.bottomAnchor.constraint(equalTo: g.bottomAnchor, constant: -8.0),
])
let names: [String] = [
"Top-Left",
"Top-Right",
"Bottom-Left",
"Bottom-Right",
"Center",
]
let colors: [UIColor] = [
.init(red: 1.0, green: 0.8, blue: 0.8, alpha: 1.0),
.init(red: 0.8, green: 1.0, blue: 0.8, alpha: 1.0),
.init(red: 0.8, green: 0.8, blue: 1.0, alpha: 1.0),
.init(red: 0.8, green: 1.0, blue: 1.0, alpha: 1.0),
.init(red: 1.0, green: 1.0, blue: 0.8, alpha: 1.0).withAlphaComponent(0.5),
]
var labels: [UILabel] = []
for (str, c) in zip(names, colors) {
let v = UILabel()
v.textAlignment = .center
v.font = .systemFont(ofSize: 40, weight: .light)
v.text = str
v.backgroundColor = c
v.frame = CGRect(x: 0, y: 0, width: w * 0.5, height: h * 0.5)
labels.append(v)
scrollView.addSubview(v)
}
labels[0].center = .init(x: w * 0.25, y: h * 0.25)
labels[1].center = .init(x: w * 0.75, y: h * 0.25)
labels[2].center = .init(x: w * 0.25, y: h * 0.75)
labels[3].center = .init(x: w * 0.75, y: h * 0.75)
labels[4].center = .init(x: w * 0.5, y: h * 0.5)
scrollView.delegate = self
fullSize = true
}
func updateInfo() {
infoCount += 1
infoLabelA.text = "\(infoCount) :: .contentSize = \(scrollView.contentSize)"
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let pt = String(format: ".contentOffset: (%0.2f, %0.2f)", scrollView.contentOffset.x, scrollView.contentOffset.y)
let sz = String(format: ".contentSize: (%0.2f, %0.2f)", scrollView.contentSize.width, scrollView.contentSize.height)
infoLabelC.text = "\(pt)\n\(sz)"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment