UIAlertController
を使用しています。しかし、iOS 8を搭載したiPadでは、アクションシートはポップオーバー矢印付きで表示されます。その矢を隠すアイデアはありますか?
これが私のコードです:
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"this is alert controller" message:@"yeah" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *cancelAction = [UIAlertAction
actionWithTitle:NSLocalizedString(@"Cancel", @"Cancel action")
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action)
{
NSLog(@"Cancel action");
}];
UIAlertAction *okAction = [UIAlertAction
actionWithTitle:NSLocalizedString(@"OK", @"OK action")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
NSLog(@"OK action");
}];
UIAlertAction *deleteAction = [UIAlertAction
actionWithTitle:NSLocalizedString(@"Delete", @"Delete action")
style:UIAlertActionStyleDestructive
handler:^(UIAlertAction *action) {
NSLog(@"Delete action");
}];
[alertController addAction:cancelAction];
[alertController addAction:okAction];
[alertController addAction:deleteAction];
UIPopoverPresentationController *popover = alertController.popoverPresentationController;
if (popover) {
popover.sourceView = self.view;
popover.sourceRect = self.view.bounds;
popover.permittedArrowDirections = UIPopoverArrowDirectionUnknown;
}
[self presentViewController:alertController animated:YES completion:nil];
ソリューション:use below line for remove arrow from action sheet
[yourAlertController.popoverPresentationController setPermittedArrowDirections:0];
サンプル
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Test Action Sheet" message:@"Message" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *cancelAction = [UIAlertAction
actionWithTitle:@"Cancel"
style:UIAlertActionStyleDestructive
handler:^(UIAlertAction *action)
{
NSLog(@"Cancel action");
}];
UIAlertAction *okAction = [UIAlertAction
actionWithTitle:@"Ok"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
NSLog(@"OK action");
}];
UIAlertAction *otherAction = [UIAlertAction
actionWithTitle:@"Other"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
NSLog(@"Otheraction");
}];
[alertController addAction:okAction];
[alertController addAction:otherAction];
[alertController addAction:cancelAction];
// Remove arrow from action sheet.
[alertController.popoverPresentationController setPermittedArrowDirections:0];
//For set action sheet to middle of view.
alertController.popoverPresentationController.sourceView = self.view;
alertController.popoverPresentationController.sourceRect = self.view.bounds;
[self presentViewController:alertController animated:YES completion:nil];
出力
SwiftでのJageenの答え:
popoverController.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)
ナビゲーション/ステータスバーがある場合、選択した回答はアラートを中央に配置しません。アラートコントローラを正確に中央に配置するには:
alertController.popoverPresentationController.sourceRect = [self sourceRectForCenteredAlertController];
alertController.popoverPresentationController.sourceView = self.view;
alertController.popoverPresentationController.permittedArrowDirections = 0;
便利な方法で:
- (CGRect)sourceRectForCenteredAlertController
{
CGRect sourceRect = CGRectZero;
sourceRect.Origin.x = CGRectGetMidX(self.view.bounds)-self.view.frame.Origin.x/2.0;
sourceRect.Origin.y = CGRectGetMidY(self.view.bounds)-self.view.frame.Origin.y/2.0;
return sourceRect;
}
また、ビューが回転してもアラートコントローラーは中央に配置されません。アラートコントローラーを中央に維持するには、回転後にsourceRectを更新する必要があります。例えば:
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
// Check if your alert controller is still being presented
if (alertController.presentingViewController) {
alertController.popoverPresentationController.sourceRect = [self sourceRectForCenteredAlertController];
}
}
または、回転イベントを使用したくない場合は、popoverPresentationController
デリゲートメソッドを使用してポップオーバーの位置を変更できます。
- (void)popoverPresentationController:(UIPopoverPresentationController *)popoverPresentationController willRepositionPopoverToRect:(inout CGRect *)rect inView:(inout UIView *__autoreleasing _Nonnull *)view
{
// Re-center with new rect
}