티스토리 뷰

728x90
"Your application has presented a UIAlertController (<UIAlertController: 0x7fc03e0bda00>) of style UIAlertControllerStyleActionSheet from App.ViewController (<App.ViewController: 0x7fc03d7257e0>). The modalPresentationStyle of a UIAlertController with this style is UIModalPresentationPopover. You must provide location information for this popover through the alert controller's popoverPresentationController. You must provide either a sourceView and sourceRect or a barButtonItem.  If this information is not known when you present the alert controller, you may provide it in the UIPopoverPresentationControllerDelegate method -prepareForPopoverPresentation."

 

아이폰에서 구동하도록 구현한 앱을 아이패드에서 구동했을때 UIAlertController의 preferredStyle을 .actionSheet로 설정해뒀을때 위와 같은 오류가 발생한다.

 

다른 자료를 찾아보면 아이패드에서 실행할 때는 위치를 지정해서 actionSheet를 띄우는 방법도 있지만

어차피 아이패드에선 굳이 alert를 밑에 띄울 필요가 없으므로.. preferredStyle을 .alert로 설정해주면 문제없이 작동한다.

 

if UIDevice.current.userInterfaceIdiom == .pad {
	alert = UIAlertController(title: "title", message: "", preferredStyle: .alert)
}

현재 디바이스가 어떤 인터페이스인지를 저장하고 있는 userInterfaceIdiom가 .pad인 경우를 위에 처럼 따로 처리해준다.

 

 

참고로 userInterfaceIdiom의 자료형은 UIUserInterfaceIdiom이라는 열거형 타입이고 종류는 unspecified, phone, pad, tv, carPlay, mac이 있다. idiom이 뭔지 모르겠어서 찾아보니 관용구,,라는데 여전히 모르겠음 뭐 그렇구나..

 

 

최종 코드

var alert: UIAlertController
if UIDevice.current.userInterfaceIdiom == .pad {
	alert = UIAlertController(title: "title1", message: "", preferredStyle: .alert)
} else {
	alert = UIAlertController(title: "title1", message: "", preferredStyle: .actionSheet)
}
let Action = UIAlertAction(title: "1", style: .default, handler: nil)
let cancel = UIAlertAction(title: "취소", style: .cancel, handler: nil)
alert.addAction(Action)
alert.addAction(cancel)
present(alert, animated: true, completion: nil)

 

728x90

'iOS' 카테고리의 다른 글

[iOS] swift tab bar 와 tool bar  (0) 2021.03.08
[iOS] swift tab bar 사용시 presentingViewController  (0) 2021.03.08
[iOS] Core Data  (0) 2021.02.13
[iOS] CollectionView  (0) 2021.02.12
[iOS] tableView  (0) 2021.02.05
250x250
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함