[swift] Swift Presentr의 사용 예시

Swift Presentr는 iOS 앱에서 모달 형태의 화면을 쉽게 구현할 수 있도록 도와주는 라이브러리입니다. 이번 포스트에서는 Swift Presentr을 사용하여 앱에서 다이얼로그 창을 표시하는 방법을 알아보겠습니다.

1. Swift Presentr 라이브러리 설치하기

먼저, Swift Presentr 라이브러리를 프로젝트에 추가해야 합니다. Podfile 파일에 다음과 같이 라이브러리를 추가합니다.

pod 'Presentr'

그리고 터미널에서 다음 명령어를 실행하여 라이브러리를 설치합니다.

pod install

2. Presentr 객체 생성하기

다이얼로그 형태의 화면을 구현하기 위해 Presentr 객체를 생성해야 합니다. Presentr 객체는 다이얼로그의 크기, 애니메이션 설정 등을 관리합니다.

import Presentr

let presenter: Presentr = {
    let customPresenter = Presentr(presentationType: .alert)
    customPresenter.transitionType = .coverVertical
    customPresenter.dismissTransitionType = .coverVertical
    return customPresenter
}()

3. 다이얼로그 창 표시하기

다이얼로그 창을 표시하기 위해 Presentr 객체를 사용합니다. 예를 들어, 다음과 같이 알림 메시지를 표시하는 다이얼로그 창을 생성할 수 있습니다.

let alertController = Presentr.alertViewController(title: "알림", body: "새로운 메시지가 도착했습니다.")
let okAction = AlertAction(title: "확인", style: .cancel) { _ in
    // 확인 버튼 눌렀을 때 실행할 로직
}

alertController.addAction(okAction)

present(alertController, animated: true, completion: nil)

이렇게 Presentr을 사용하여 앱에서 다이얼로그 창을 표시할 수 있습니다. Presentr의 다양한 옵션을 통해 사용자 정의 형태의 다이얼로그를 만들 수도 있습니다. 자세한 내용은 Swift Presentr GitHub 페이지를 참고하세요.