Skip to content

Instantly share code, notes, and snippets.

@maxhand
Last active March 7, 2024 20:10
Show Gist options
  • Save maxhand/24f9a69dc83705cfc025cae63f86a03c to your computer and use it in GitHub Desktop.
Save maxhand/24f9a69dc83705cfc025cae63f86a03c to your computer and use it in GitHub Desktop.
import UIKit
class BackgroundView: UIView {
private let containedView = UIStackView()
private var buttonAction: (() -> ())?
override init(frame: CGRect) {
super.init(frame: frame)
containedView.alignment = .center
containedView.axis = .vertical
containedView.spacing = 4
setupView()
}
private func setupView() {
addSubview(containedView)
containedView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
containedView.centerXAnchor.constraint(equalTo: centerXAnchor),
containedView.centerYAnchor.constraint(equalTo: centerYAnchor),
containedView.widthAnchor.constraint(equalToConstant: 250)
])
}
func configure(using representable: ListBackgroundViewRepresentable?) {
guard let representable else { return }
let symbolConfiguration = UIImage.SymbolConfiguration(scale: .large)
let imageView = UIImageView(image: UIImage(systemName: representable.systemImageName)?.withConfiguration(symbolConfiguration).withTintColor(.systemGray2).withRenderingMode(.alwaysOriginal))
let titleLabel = UILabel()
titleLabel.font = .preferredFont(forTextStyle: .headline)
titleLabel.textColor = .systemGray
titleLabel.adjustsFontForContentSizeCategory = true
titleLabel.numberOfLines = 0
titleLabel.text = representable.title
let secondaryLabel = UILabel()
secondaryLabel.font = .preferredFont(forTextStyle: .body)
secondaryLabel.text = representable.body
secondaryLabel.numberOfLines = 0
secondaryLabel.textColor = .systemGray2
secondaryLabel.textAlignment = .center
let accessoryButton = UIButton()
var accessoryButtonConfig: UIButton.Configuration = .filled()
accessoryButtonConfig.title = representable.buttonTitle
accessoryButtonConfig.cornerStyle = .capsule
accessoryButton.layer.cornerRadius = 15
accessoryButton.addTarget(self, action: #selector(handleButtonPressed), for: .touchUpInside)
accessoryButton.configuration = accessoryButtonConfig
accessoryButton.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
accessoryButton.widthAnchor.constraint(equalToConstant: 110),
accessoryButton.heightAnchor.constraint(equalToConstant: 35)
])
var views = [imageView, titleLabel, secondaryLabel]
if representable.buttonAction != nil && representable.buttonTitle != nil {
views.append(accessoryButton)
}
buttonAction = representable.buttonAction
for view in views {
containedView.addArrangedSubview(view)
}
containedView.setCustomSpacing(8, after: imageView)
containedView.setCustomSpacing(20, after: secondaryLabel)
}
@objc private func handleButtonPressed(sender: UIButton) {
buttonAction?()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
let backgroundView = BackgroundView()
backgroundView.configure(
using: ListBackgroundView(
systemImageName: "line.3.horizontal.decrease",
title: "These are not the results that you are looking for",
buttonTitle: "Energize",
buttonAction: {
// Do great things
}
)
)
struct ListBackgroundView: ListBackgroundViewRepresentable {
var systemImageName: String
var title: String
var body: String?
var buttonTitle: String?
var buttonAction: (() -> ())?
}
protocol ListBackgroundViewRepresentable {
var systemImageName: String { get }
var title: String { get }
var body: String? { get }
var buttonTitle: String? { get }
var buttonAction: (() -> ())? { get }
}
@maxhand
Copy link
Author

maxhand commented Mar 7, 2024

Looks great! Happy to help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment