Skip to content

Instantly share code, notes, and snippets.

@vurgunmert
Created August 23, 2024 09:41
Show Gist options
  • Save vurgunmert/23789b94984a4ca62a4ef3702827bb77 to your computer and use it in GitHub Desktop.
Save vurgunmert/23789b94984a4ca62a4ef3702827bb77 to your computer and use it in GitHub Desktop.
UIKit Alerts Storybook tvOS
//
// UIKitAlertsStorybook.swift
// TemplateAppTvOS
//
// Created by Mert Vurgun on 23.08.2024.
//
import UIKit
class UIKitAlertsStorybook: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .black
setupButtons()
}
private func setupButtons() {
let buttonTitles = [
"Basic Alert",
"Destructive Alert",
"Action Sheet",
"Text Input Alert",
"Custom Alert"
]
let actions: [Selector] = [
#selector(showBasicAlert),
#selector(showDestructiveAlert),
#selector(showActionSheet),
#selector(showTextInputAlert),
#selector(showCustomAlert)
]
let stackView = UIStackView()
stackView.axis = .vertical
stackView.alignment = .center
stackView.distribution = .equalSpacing
stackView.spacing = 20
for (title, action) in zip(buttonTitles, actions) {
let button = createButton(title: title, action: action)
stackView.addArrangedSubview(button)
}
stackView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(stackView)
NSLayoutConstraint.activate([
stackView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
stackView.centerYAnchor.constraint(equalTo: view.centerYAnchor)
])
}
private func createButton(title: String, action: Selector) -> UIButton {
let button = UIButton(type: .system)
button.setTitle(title, for: .normal)
button.titleLabel?.font = UIFont.systemFont(ofSize: 18, weight: .medium)
button.addTarget(self, action: action, for: .primaryActionTriggered)
return button
}
// MARK: - Alert Show Methods
@objc private func showBasicAlert() {
let alert = UIAlertController(title: "Basic Alert", message: "This is a basic alert with OK and Cancel buttons.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
present(alert, animated: true, completion: nil)
}
@objc private func showDestructiveAlert() {
let alert = UIAlertController(title: "Delete Item", message: "Are you sure you want to delete this item?", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Delete", style: .destructive, handler: { _ in
print("Item Deleted")
}))
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
present(alert, animated: true, completion: nil)
}
@objc private func showActionSheet() {
let actionSheet = UIAlertController(title: "Choose an Option", message: nil, preferredStyle: .actionSheet)
actionSheet.addAction(UIAlertAction(title: "Option 1", style: .default, handler: { _ in
print("Option 1 selected")
}))
actionSheet.addAction(UIAlertAction(title: "Option 2", style: .default, handler: { _ in
print("Option 2 selected")
}))
actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
present(actionSheet, animated: true, completion: nil)
}
@objc private func showTextInputAlert() {
let alert = UIAlertController(title: "Input Required", message: "Please enter your name.", preferredStyle: .alert)
alert.addTextField { textField in
textField.placeholder = "Name"
}
alert.addAction(UIAlertAction(title: "Submit", style: .default, handler: { _ in
if let text = alert.textFields?.first?.text {
print("User entered: \(text)")
}
}))
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
present(alert, animated: true, completion: nil)
}
@objc private func showCustomAlert() {
let alert = UIAlertController(title: nil, message: nil, preferredStyle: .alert)
let customView = UIView(frame: CGRect(x: 0, y: 0, width: 1100, height: 500))
customView.backgroundColor = .purple
customView.layer.cornerRadius = 16
let titleLabel = UILabel()
titleLabel.text = "Custom Alert"
titleLabel.textColor = .white
titleLabel.font = UIFont.systemFont(ofSize: 32, weight: .bold)
titleLabel.translatesAutoresizingMaskIntoConstraints = false
let messageLabel = UILabel()
messageLabel.text = "This is a custom-styled alert message."
messageLabel.textColor = .white
messageLabel.numberOfLines = 0
messageLabel.textAlignment = .center
messageLabel.font = UIFont.systemFont(ofSize: 22, weight: .regular)
messageLabel.translatesAutoresizingMaskIntoConstraints = false
customView.addSubview(titleLabel)
customView.addSubview(messageLabel)
NSLayoutConstraint.activate([
titleLabel.topAnchor.constraint(equalTo: customView.topAnchor, constant: 12),
titleLabel.centerXAnchor.constraint(equalTo: customView.centerXAnchor),
messageLabel.leadingAnchor.constraint(equalTo: customView.leadingAnchor, constant: 20),
messageLabel.trailingAnchor.constraint(equalTo: customView.trailingAnchor, constant: -20),
messageLabel.bottomAnchor.constraint(equalTo: customView.bottomAnchor, constant: -32)
])
alert.view.addSubview(customView)
alert.view.sendSubviewToBack(customView)
let heightConstraint = NSLayoutConstraint(item: alert.view!, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 400)
alert.view.addConstraint(heightConstraint)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { _ in
print("OK pressed")
}))
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { _ in
print("Cancel pressed")
}))
present(alert, animated: true, completion: nil)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment