Skip to content

Instantly share code, notes, and snippets.

@djryanash
Created May 12, 2023 09:51
Show Gist options
  • Save djryanash/67db78743c95505f2a5303f9c96c735e to your computer and use it in GitHub Desktop.
Save djryanash/67db78743c95505f2a5303f9c96c735e to your computer and use it in GitHub Desktop.
Various UIKit controls which interact with each other and are created programmatically.
import UIKit
class HomeViewController: UIViewController {
var textField: UITextFieldPadded = {
let textField = UITextFieldPadded()
textField.translatesAutoresizingMaskIntoConstraints = false
textField.placeholder = "Enter some text here"
return textField }()
var label: UILabelPadded = {
let label = UILabelPadded()
label.translatesAutoresizingMaskIntoConstraints = false
label.backgroundColor = .systemBackground
label.text = "This is a label."
label.adjustsFontForContentSizeCategory = true
label.numberOfLines = 0
return label }()
var slider: UISlider = {
let slider = UISlider()
slider.translatesAutoresizingMaskIntoConstraints = false
slider.backgroundColor = .systemGray
slider.maximumValue = 100
slider.minimumValue = 0
slider.accessibilityIdentifier = "Completion"
return slider }()
var progressView: UIProgressView = {
let progressView = UIProgressView()
progressView.translatesAutoresizingMaskIntoConstraints = false
progressView.backgroundColor = .systemMint
progressView.progressTintColor = .systemRed
return progressView }()
var segment: UISegmentedControl = {
let segment = UISegmentedControl()
segment.translatesAutoresizingMaskIntoConstraints = false
segment.insertSegment(withTitle: "Omega", at: 0, animated: true)
segment.insertSegment(withTitle: "Alpha", at: 0, animated: true)
segment.selectedSegmentIndex = 0
return segment }()
var stack: UIStackView = {
let stack = UIStackView()
stack.translatesAutoresizingMaskIntoConstraints = false
stack.backgroundColor = .systemOrange
stack.alignment = .center
stack.layer.cornerRadius = 12
stack.layer.masksToBounds = true
let redButton = UIButton()
redButton.configuration = .plain()
redButton.backgroundColor = .systemRed
redButton.setTitleColor(.black, for: .normal)
redButton.setTitle("Red", for: .normal)
let greenButton = UIButton()
greenButton.configuration = .plain()
greenButton.backgroundColor = .systemGreen
greenButton.setTitleColor(.black, for: .normal)
greenButton.setTitle("Green", for: .normal)
let blueButton = UIButton()
blueButton.configuration = .plain()
blueButton.backgroundColor = .systemBlue
blueButton.setTitleColor(.black, for: .normal)
blueButton.setTitle("Blue", for: .normal)
stack.addArrangedSubview(redButton)
stack.addArrangedSubview(greenButton)
stack.addArrangedSubview(blueButton)
return stack }()
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .secondarySystemBackground
view.addSubview(textField)
view.addSubview(label)
view.addSubview(slider)
view.addSubview(progressView)
view.addSubview(segment)
view.addSubview(stack)
self.title = "Alpha"
configure()
}
func segmentChange(_ sender: UISegmentedControl) {
title = segment.titleForSegment(at: segment.selectedSegmentIndex)
}
@objc func textChanged(_ sender: UITextField) {
label.text = textField.text
}
@objc func sliderDragged(_ sender: UISlider) {
let progress = ( sender.maximumValue / sender.maximumValue ) - ( sender.value / sender.maximumValue )
progressView.setProgress(progress, animated: true)
}
@objc func stackButtonTapped(_ sender: UIButton) {
let alert = UIAlertController(title: sender.currentTitle, message: nil, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default))
present(alert, animated: true)
}
@objc func colorSelected(_ sender: UIButton) {
let alert = UIAlertController(title: sender.titleLabel?.text, message: nil, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default))
present(alert, animated: true)
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
func configure() {
textField.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor).isActive = true
textField.leadingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.leadingAnchor,
constant: self.view.bounds.width / 10).isActive = true
textField.trailingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.trailingAnchor,
constant: -(self.view.bounds.width / 10)).isActive = true
textField.addTarget(self, action: #selector(textChanged(_:)), for: .allEditingEvents)
label.topAnchor.constraint(equalTo: textField.bottomAnchor, constant: 50).isActive = true
label.leadingAnchor.constraint(equalTo: textField.leadingAnchor).isActive = true
label.trailingAnchor.constraint(equalTo: textField.trailingAnchor).isActive = true
label.heightAnchor.constraint(equalTo: textField.heightAnchor).isActive = true
slider.topAnchor.constraint(equalTo: label.bottomAnchor, constant: 50).isActive = true
slider.leadingAnchor.constraint(equalTo: textField.leadingAnchor).isActive = true
slider.trailingAnchor.constraint(equalTo: textField.trailingAnchor).isActive = true
slider.addTarget(self, action: #selector(sliderDragged(_:)), for: .touchDragInside)
progressView.topAnchor.constraint(equalTo: slider.bottomAnchor, constant: 50).isActive = true
progressView.leadingAnchor.constraint(equalTo: textField.leadingAnchor).isActive = true
progressView.trailingAnchor.constraint(equalTo: textField.trailingAnchor).isActive = true
progressView.heightAnchor.constraint(equalTo: slider.heightAnchor).isActive = true
progressView.progress = ((slider.maximumValue / slider.value ) / slider.maximumValue)
segment.topAnchor.constraint(equalTo: progressView.bottomAnchor, constant: 50).isActive = true
segment.leadingAnchor.constraint(equalTo: textField.leadingAnchor).isActive = true
segment.trailingAnchor.constraint(equalTo: textField.trailingAnchor).isActive = true
segment.addTarget(self, action: #selector(handleSegment(_:)), for: .valueChanged)
stack.topAnchor.constraint(equalTo: segment.bottomAnchor, constant: 50).isActive = true
stack.leadingAnchor.constraint(equalTo: textField.leadingAnchor).isActive = true
stack.trailingAnchor.constraint(equalTo: textField.trailingAnchor).isActive = true
for sub in stack.arrangedSubviews {
if sub is UIButton {
let button = sub as? UIButton
button?.addTarget(self, action: #selector(stackButtonTapped(_:)), for: .touchUpInside)
}
}
}
@objc func handleSegment(_ sender: UISegmentedControl) {
self.title = sender.titleForSegment(at: sender.selectedSegmentIndex)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment