[swift] Swift에서 PopupDialog를 사용하여 목표 달성 현황 팝업창 만들기

서론

앱 개발 중에는 사용자와 상호작용하는 팝업창을 구현해야 할 때가 있습니다. 이번 포스트에서는 Swift에서 PopupDialog 라이브러리를 사용하여 목표 달성 현황 팝업창을 만드는 방법에 대해 알아보겠습니다.

PopupDialog 라이브러리

PopupDialog는 Swift에서 사용할 수 있는 팝업창 관련 라이브러리입니다. 많은 사용자 인터페이스 요소를 제공하며, 사용하기 쉽고 커스터마이징할 수 있습니다.

설치

PopupDialog는 CocoaPods을 통해 설치할 수 있습니다. Podfile에 아래와 같이 라이브러리를 추가합니다.

pod 'PopupDialog'

그리고 터미널에서 pod install 명령어를 실행하여 의존성을 설치합니다.

사용법

  1. 먼저 PopupDialog를 import합니다.
import PopupDialog
  1. 팝업창에 표시할 컨텐츠를 준비합니다. 일반적으로는 UILabel, UIImageView, UITextView 등의 요소를 사용합니다.
let title = "목표 달성 현황"
let message = "현재까지의 목표 달성 현황입니다."

let titleLabel = UILabel()
titleLabel.text = title
titleLabel.font = .boldSystemFont(ofSize: 20)

let messageLabel = UILabel()
messageLabel.text = message
messageLabel.font = .systemFont(ofSize: 16)
messageLabel.numberOfLines = 0

let contentViewController = UIViewController()
contentViewController.view.addSubview(titleLabel)
contentViewController.view.addSubview(messageLabel)

titleLabel.translatesAutoresizingMaskIntoConstraints = false
messageLabel.translatesAutoresizingMaskIntoConstraints = false

NSLayoutConstraint.activate([
    titleLabel.topAnchor.constraint(equalTo: contentViewController.view.topAnchor, constant: 16),
    titleLabel.leadingAnchor.constraint(equalTo: contentViewController.view.leadingAnchor, constant: 16),
    titleLabel.trailingAnchor.constraint(equalTo: contentViewController.view.trailingAnchor, constant: -16),
    
    messageLabel.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: 8),
    messageLabel.leadingAnchor.constraint(equalTo: titleLabel.leadingAnchor),
    messageLabel.trailingAnchor.constraint(equalTo: titleLabel.trailingAnchor),
    messageLabel.bottomAnchor.constraint(equalTo: contentViewController.view.bottomAnchor, constant: -16)
])
  1. 팝업창을 생성하고 컨텐츠를 설정합니다.
let popup = PopupDialog(viewController: contentViewController, buttonAlignment: .horizontal, transitionStyle: .fadeIn, tapGestureDismissal: true)
  1. 필요한 경우 버튼을 추가합니다.
let cancelButton = DefaultButton(title: "취소") {
    // 취소 버튼을 눌렀을 때 실행할 동작
}

let confirmButton = DefaultButton(title: "확인") {
    // 확인 버튼을 눌렀을 때 실행할 동작
}

popup.addButtons([cancelButton, confirmButton])
  1. 팝업창을 표시합니다.
present(popup, animated: true, completion: nil)

마무리

이번 포스트에서는 Swift에서 PopupDialog를 사용하여 목표 달성 현황 팝업창을 만드는 방법을 알아보았습니다. PopupDialog 라이브러리는 사용하기 쉽고 다양한 커스터마이징이 가능하여 선택할 가치가 있습니다. 앱 개발에서 팝업창을 구현해야 할 경우 PopupDialog를 적극 활용해보세요!

참고 자료