UIActionSheet iPhone/iPad 적용

최대 1 분 소요

2020.04.22

UIActionSheet 이란

쉽게 말하면 화면에 버튼 목록을 말한다. 아이폰 사용자라면 밑의 이미지가 익숙할 것이다.

출처 : https://www.ioscreator.com/tutorials/swiftui-action-sheet-tutorial

Important: UIActionSheet is deprecated in iOS 8. (Note that UIAction
SheetDelegate is also deprecated.)To create and manage action sheets in 
iOS 8 and later, instead use UIAlertController with a preferred
Style of UIAlertControllerStyleActionSheet.

UIActionSheet이 iOS 8.3 이후부터는 사용할 수 없다…
대신에 UIAlertController를 사용하고, preferredStyleUIAlertControllerStyleActionSheet을 설정해야 한다.

iPhone에 UIActionSheet 적용

UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleActionSheet];

UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
    NSLog(@"OK");
}];
[actionSheet addAction:okAction];
[actionSheet show];

iPad에 UIActionSheet 적용

iPad에 적용할 경우, popoverPresentationController를 사용해야 한다.
화면 가운데에 출력학 위해서 CGRect x,y 좌표를 설정해야 한다.

UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleActionSheet];

UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
    NSLog(@"OK");
}];
[actionSheet addAction:okAction];
[actionSheet setModalPresentationStyle:UIModalPresentationPopover];
[actionSheet.popoverPresentationController setPermittedArrorDirections:0];

CGRect sourceRect = CGRectZero;
sourceRect.origin.x = CGRectGetMidX(actionSheet.view.bounds) - actionSheet.view.frame.origin.x/2.0;
sourceRect.origin.y = CGRectGetMidY(actionSheet.view.bounds) - actionSheet.view.frame.origin.y/2.0;

popoverPresentationController.sourceRect = sourceRect;
popoverPresentationController.sourceView = actionSheet.view;

[actionSheet show];

카테고리:

업데이트:

댓글남기기