티스토리 뷰

iOS/iOS 개발

iOS ActionSheet 만들기

포도 동 2021. 1. 14. 16:17

애플 HIG 에 ActionSheet는 "현재 컨텍스트와 관련된 두 개 이상의 선택 항목을 표시하는 특정 유형의 경고"라고 설명합니다.

 

iOS 에서 ActionSheet 는 UIAlertController를 이용해 만들 수 있습니다.

let actionSheetController 
	= UIAlertController(title: "제목", message: "메세지", preferredStyle: .actionSheet)
    
//제목, 메세지 없이
let actionSheetController 
	= UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)

먼저 UIAlertController에서 actionSheet 스타일을 선택해 ActionSheet를 만듭니다.

*제목 또는 메시지가 없는 ActionSheet를 만들고싶으면 nil로 작성하면 됩니다! ""로 표현하면 빈 공간으로 나오더라구요...

 

let firstAction = UIAlertAction(title: "기능 1", style: .default, handler: {
//원하는 기능 구현
})
let secondAction = UIAlertAction(title: "기능2", style: .default, handler: {
//원하는 기능 구현
})

let destructiveAction = UIAlertAction(title: "destructive", style: .destructive, handler: nil)
let cancelAction = UIAlertAction(title: "취소", style: .cancel, handler: nil)

그 후 원하는 기능을 UIAlertAction로 만들어줍니다.

스타일은 일반 기능(default), 삭제 기능 등 (destructive) , 취소 기능 (cancel)이 있습니다.

각 기능의 handler를 통해 원하는 기능을 처리해 주면 됩니다.

하나의 ActionSheet에 취소 기능은 하나만 포함할 수 있습니다!

 

//Controller에 기능 추가
actionSheetController.addAction(firstAction)
actionSheetController.addAction(secondAction)
actionSheetController.addAction(destructiveAction)
actionSheetController.addAction(cancelAction)

//present로 Controller 호출
self.present(actionSheetController, animated: true, completion: nil)

맨 처음 만든 actionSheetController에, addAction을 이용해 기능을 추가해줍니다.

기능을 포함한 ActionSheetController는 present를 이용해 호출할 수 있습니다.

 

(왼쪽) 제목,메시지, destructive 포함 (오른쪽) 제목, 메시지, destructive 없음

실행시키면 위처럼 액션 시트를 이용할 수 있게 됩니다!

 

 

* 전체코드

let actionSheetController 
	= UIAlertController(title: "제목", message: "메세지", preferredStyle: .actionSheet)

let firstAction = UIAlertAction(title: "기능 1", style: .default, handler: {
//원하는 기능 구현
})
let secondAction = UIAlertAction(title: "기능2", style: .default, handler: {
//원하는 기능 구현
})

let destructiveAction = UIAlertAction(title: "destructive", style: .destructive, handler: nil)
let cancelAction = UIAlertAction(title: "취소", style: .cancel, handler: nil)

actionSheetController.addAction(firstAction)
actionSheetController.addAction(secondAction)
actionSheetController.addAction(destructiveAction)
actionSheetController.addAction(cancelAction)

self.present(actionSheetController, animated: true, completion: nil)

 

참고:

https://developer.apple.com/design/human-interface-guidelines/ios/views/action-sheets/

https://developer.apple.com/documentation/uikit/uialertcontroller

'iOS > iOS 개발' 카테고리의 다른 글

iOS - BlendShapes 활용하기  (0) 2021.05.27
ARKit - BlendShapes  (0) 2021.05.25
iOS - Dispatch Queue  (0) 2021.05.21
iOS앱 Lottie 애니메이션 구현하기  (0) 2020.11.29
XCODE프로젝트에 코코아팟(CocoaPods) 설치하기  (0) 2020.11.29
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/08   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
글 보관함