[swift] Swift Presentr에 대한 실제 사용 사례

Swift Presentr은 iOS 애플리케이션에서 모달 프레젠테이션을 쉽게 구현할 수 있는 라이브러리입니다. 이번 포스트에서는 Swift Presentr을 사용하여 구현한 실제 사용 사례를 살펴보겠습니다.

프로젝트 설치

먼저 Swift Presentr을 프로젝트에 설치해야 합니다. CocoaPods을 사용하는 경우, Podfile에 다음과 같이 추가해주세요.

pod 'Presentr'

설치를 완료한 후, import Presentr 구문을 사용하여 Presentr을 사용할 준비를 해야 합니다.

사용 사례

  1. 간단한 Alert 표시
import UIKit
import Presentr

class ViewController: UIViewController {
    
    let presentr: Presentr = {
        let appearance = PresentrAppearance()
        appearance.backgroundColor = .white
        appearance.cornerRadius = 10
        return Presentr(presentationType: .alert, appearance: appearance)
    }()

    @IBAction func showAlertButtonTapped(_ sender: UIButton) {
        let alertVC = UIAlertController(title: "알림", message: "안녕하세요!", preferredStyle: .alert)
        let doneAction = UIAlertAction(title: "확인", style: .default, handler: nil)
        alertVC.addAction(doneAction)
        
        present(alertVC, animated: true, completion: nil)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

위의 예제 코드에서는 Presentr의 alert 프레젠테이션 타입을 사용하여 간단한 Alert를 표시하는 것을 보여줍니다. 새로운 UIAlertController 인스턴스를 생성하고, Presentr의 present 메서드를 사용하여 Alert를 모달로 표시합니다.

  1. 커스텀 프레젠테이션 스타일
import UIKit
import Presentr

class ViewController: UIViewController {
    
    let presentr: Presentr = {
        let appearance = PresentrAppearance()
        appearance.backgroundColor = .white
        appearance.cornerRadius = 10
        return Presentr(presentationType: .popup, appearance: appearance)
    }()

    @IBAction func showPopupButtonTapped(_ sender: UIButton) {
        let popupVC = PopupViewController()
        present(popupVC, animated: true, completion: nil)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

class PopupViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .white
        self.view.layer.cornerRadius = 10
        
        // Add custom UI elements and layout constraints
        
        let closeButton = UIButton()
        closeButton.setTitle("Close", for: .normal)
        closeButton.addTarget(self, action: #selector(closeButtonTapped), for: .touchUpInside)
        self.view.addSubview(closeButton)
        
        // Add layout constraints for closeButton and other elements
        
    }
    
    @objc func closeButtonTapped() {
        dismiss(animated: true, completion: nil)
    }
}

위의 예제 코드에서는 Presentr의 popup 프레젠테이션 타입을 사용하여 커스텀 팝업을 표시하는 것을 보여줍니다. 새로운 PopupViewController를 생성하고, Presentr의 present 메서드를 사용하여 팝업을 모달로 표시합니다. 팝업 뷰 컨트롤러에서는 원하는 대로 UI 요소를 추가하고 레이아웃 제약을 설정할 수 있습니다.

결론

Swift Presentr은 iOS 애플리케이션에서 간편하게 모달 프레젠테이션을 구현할 수 있도록 도와줍니다. 실제 사용 사례를 통해 Presentr의 기본 사용법과 커스텀 활용 방법을 알아보았습니다. 앞으로 여러분의 앱에서 Presentr을 사용하여 사용자 경험을 향상시킬 수 있을 것입니다.

참고 자료: