iOS 앱을 개발하다 보면 알림(Alert) 창의 너비나 높이를 직접 조절해야 하는 경우가 종종 발생합니다. 이 작업에 익숙하지 않다면 다소 까다롭게 느껴질 수 있는데요. 이번 글에서는 NSLayoutConstraint를 활용해 기본 알림 대화 상자의 크기를 자유롭게 제어하는 방법을 단계별로 살펴보겠습니다.
UIAlertController란?
UIAlertController는 iOS에서 사용자에게 메시지를 표시하거나 간단한 선택지를 제공할 때 사용하는 표준 UI 컴포넌트입니다. 자세한 내용은 Apple 공식 문서를 참고하세요.
https://developer.apple.com/documentation/uikit/uialertcontroller
이번 예제에서는 새 프로젝트를 생성하고 버튼 하나를 배치한 뒤, 해당 버튼을 탭하면 커스텀 메시지가 담긴 알림 창이 나타나도록 구현해 보겠습니다.
구현 단계
1단계: 프로젝트 생성
Xcode를 실행한 후 New Project → Single View Application을 선택하고 프로젝트 이름을 "changeheightandwidth"로 지정합니다.
2단계: 버튼 추가 및 아울렛 연결
Main.storyboard에 버튼을 하나 추가하고 이름을 "tap"으로 지정합니다. 그런 다음 ViewController.swift에 @IBAction을 생성하고 btnATap이라는 이름으로 연결합니다.
3단계: 버튼 메서드에 코드 작성
먼저 UIAlertController 객체를 생성합니다.
let alert = UIAlertController(title: "Your Title", message: "Your Message", preferredStyle: UIAlertController.Style.alert)
다음으로 높이와 너비에 대한 NSLayoutConstraint를 생성하고 알림 뷰에 추가합니다.
// 높이 제약 조건
let constraintHeight = NSLayoutConstraint(
item: alert.view!, attribute: NSLayoutConstraint.Attribute.height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute:
NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, constant: 100)
alert.view.addConstraint(constraintHeight)
// 너비 제약 조건
let constraintWidth = NSLayoutConstraint(
item: alert.view!, attribute: NSLayoutConstraint.Attribute.width, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute:
NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, constant: 300)
alert.view.addConstraint(constraintWidth)마지막으로 취소(Cancel)와 확인(Done) 액션을 추가한 뒤 알림 창을 화면에 표시합니다.
let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) alert.addAction(cancel) let OKAY = UIAlertAction(title: "Done", style: .default, handler: nil) alert.addAction(OKAY) self.present(alert, animated: true, completion: nil)
4단계: 실행 및 확인
코드를 실행하면 버튼을 탭했을 때 지정한 크기(너비 300pt, 높이 100pt)의 알림 창이 나타나는 것을 확인할 수 있습니다.
전체 코드
참고를 위해 완성된 전체 코드를 정리하면 다음과 같습니다.
@IBAction func btnATap(_ sender: Any) {
let alert = UIAlertController(title: "Your Title", message: "Your Message", preferredStyle: UIAlertController.Style.alert)
// 높이 제약 조건
let constraintHeight = NSLayoutConstraint(
item: alert.view!, attribute: NSLayoutConstraint.Attribute.height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute:
NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, constant: 100)
alert.view.addConstraint(constraintHeight)
// 너비 제약 조건
let constraintWidth = NSLayoutConstraint(
item: alert.view!, attribute: NSLayoutConstraint.Attribute.width, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute:
NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, constant: 300)
alert.view.addConstraint(constraintWidth)
let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alert.addAction(cancel)
let OKAY = UIAlertAction(title: "Done", style: .default, handler: nil)
alert.addAction(OKAY)
self.present(alert, animated: true, completion: nil)
}constant 값을 변경하면 원하는 크기로 얼마든지 조정할 수 있으니, 프로젝트 요구 사항에 맞게 수치를 바꿔가며 테스트해 보세요.