UIKit의 팝업 커스텀 뷰

Jan 10 2023
UIKit에서 팝업은 주로 다음과 같습니다. 다음은 UIKit에서 사용자 정의 팝업을 만드는 방법의 예입니다. ViewController 클래스에는 버튼을 탭할 때 호출되는 showPopupButtonTapped 작업 메서드가 있습니다. PopupViewController의 인스턴스를 생성하고 모달로 표시합니다.

UIKit에서 팝업은 주로 다음과 같습니다.

  • 다음을 사용하여 알림 팝업UIAlertController
  • 를 사용한 팝업 액션 시트 UIAlertController. 자세한 내용은 다음을 확인하세요. UIKit 및 SwiftUI에 작업 시트 표시
  • 커스텀 view/viewController 팝업

다음은 UIKit에서 사용자 지정 팝업을 만드는 방법의 예입니다.

// custom popup viewcontroller
class PopupViewController: UIViewController {
  var dismissHandler: (() -> Void)?

  override func viewDidLoad() {
    super.viewDidLoad()

    // Customize the appearance of the view here
    view.backgroundColor = .white
  }

  @IBAction func dismissButtonTapped(_ sender: Any) {
    dismissHandler?()
    dismiss(animated: true, completion: nil)
  }
}

// How to use
class ViewController: UIViewController {
  @IBAction func showPopupButtonTapped(_ sender: Any) {
    let popup = PopupViewController()
    popup.dismissHandler = {
      // Update the UI or perform any other actions when the popup is dismissed
    }
    present(popup, animated: true, completion: nil)
  }
}

이 ViewController클래스에는 showPopupButtonTapped버튼을 누를 때 호출되는 작업 메서드가 있습니다. 의 인스턴스를 생성하고 PopupViewController모달로 표시합니다.

팝업을 닫으려면 에서 dismiss(_:animated:completion:)메서드를 호출합니다 PopupViewController. 팝업 이 dismissHandler닫히기 전에 클로저가 실행되고 팝업이 보기 계층에서 제거됩니다.

에 추가 보기 또는 컨트롤을 추가하거나 PopupViewController사용자 지정 모달 프레젠테이션 스타일을 사용하여 팝업의 모양과 동작을 사용자 지정할 수 있습니다.

원래 2023년 1월 9일 에 https://needone.app 에 게시되었습니다.