iOS/UIKit

[iOS / UIKit] How to use Action Sheets

TDCIAN 2023. 3. 5. 19:16

iOS에서 제공하는 액션 시트 활용법에 대해 알아봅시다!

https://developer.apple.com/design/human-interface-guidelines/components/presentation/action-sheets/

 

Action sheets - Presentation - Components - Human Interface Guidelines - Design - Apple Developer

Action sheets An action sheet is a modal view that presents choices related to an action people initiate. DEVELOPER NOTE When you use SwiftUI, you can enable action sheet functionality in all platforms by specifying a presentation modifier for a confirmati

developer.apple.com

 

바로 코드부터 봅시다!

 

//
// ViewController.swift
// HowToUseActionSheets
//
// Created by JeongminKim on 2023/03/01.
//
import UIKit
class ViewController: UIViewController {
private lazy var button: UIButton = {
let button = UIButton()
button.translatesAutoresizingMaskIntoConstraints = false
button.setTitle("Show Action Sheets", for: .normal)
button.setTitleColor(.white, for: .normal)
button.backgroundColor = .systemBlue
button.layer.cornerRadius = 16
button.addTarget(
self,
action: #selector(showActionSheets),
for: .touchUpInside
)
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(button)
NSLayoutConstraint.activate([
button.centerXAnchor.constraint(equalTo: view.centerXAnchor),
button.centerYAnchor.constraint(equalTo: view.centerYAnchor),
button.widthAnchor.constraint(equalToConstant: 200),
button.heightAnchor.constraint(equalToConstant: 100),
])
}
@objc private func showActionSheets() {
let actionSheet = UIAlertController(
title: "Action Sheet Title",
message: "Action Sheet Message",
preferredStyle: .actionSheet
)
let first = UIAlertAction(
title: "First Alert Action",
style: .default
) { action in
print("Choose First Alert Action")
}
let second = UIAlertAction(
title: "Second Alert Action",
style: .default
) { action in
print("Choose Second Alert Action")
}
let third = UIAlertAction(
title: "Third Alert Action",
style: .default
) { action in
print("Choose Third Alert Action")
}
let cancel = UIAlertAction(
title: "Cancel Alert Action",
style: .cancel
) { action in
print("Choose Cancel Alert Action")
}
[first, second, third, cancel].forEach {
actionSheet.addAction($0)
}
present(actionSheet, animated: true, completion: nil)
}
}

 

 

 

감이 잡히시나요?

 

GitHub에 Code를 올려두었으니 

다운로드 받으신 후 직접 작동해보시면 더욱 이해하기 좋을 것 같습니다!

 

 

https://github.com/TDCIAN/HowToUseActionSheets

 

GitHub - TDCIAN/HowToUseActionSheets

Contribute to TDCIAN/HowToUseActionSheets development by creating an account on GitHub.

github.com