2016-09-27 5 views
0

私はiOSに新しく、UIActionSheetに関する問題に直面しています。iphoneと同じipadの底面にUIActionSheetを表示する方法

私のコードは、この

-(IBAction)picimgbtnClick:(id)sender 
{ 
    UIActionSheet *popup = [[UIActionSheet alloc] initWithTitle:@"Profile Photo" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles: 
          @"Gallery", 
          @"Camera", 
          @"Remove Photo", 
          nil]; 


    popup.tag = 1; 
    [popup showInView:self.view]; 
} 

これは、iPhone上で完璧に動作しますが、私はiPad上でこれを実行すると、UIActionSheet中央に表示されます(下の画像を参照)、またキャンセル示していないようですボタン:iPhoneで

enter image description here

、それは次のように表示されます:

enter image description here

+0

ipadのデフォルトの動作です。あなたはそれを変更することはできません –

+0

@バルカン? – Muju

+1

あなたはUIPopoverControllerを使用することができます。 –

答えて

0

アクションシートをiPadの下部から表示することはできません。できることは警告を表示することです。

UIActionSheetクラスは、iOS 8以降廃止されており、今後は使用しないでください。代わりにUIAlertControllerを使用する必要があります。これは、ほぼ同じように動作します:

UIAlertController *alert = 
    [UIAlertController alertControllerWithTitle:@"Profile Photo" 
             message:@"Select method of input" 
           preferredStyle:UIAlertControllerStyleAlert]; 

[alert addAction:[UIAlertAction actionWithTitle:@"Gallery" 
              style:UIAlertActionStyleDefault 
             handler:^void (UIAlertAction *action) { 
              NSLog(@"Clicked Gallery"); 
             }]]; 
[alert addAction:[UIAlertAction actionWithTitle:@"Camera" 
              style:UIAlertActionStyleDefault 
             handler:^void (UIAlertAction *action) { 
              NSLog(@"Clicked Camera"); 
             }]]; 
[alert addAction:[UIAlertAction actionWithTitle:@"Remove Photo" 
              style:UIAlertActionStyleDefault 
             handler:^void (UIAlertAction *action) { 
              NSLog(@"Clicked Remove Photo"); 
             }]]; 

[self presentViewController:alert animated:YES completion:nil]; 

これは、iPhone上の警告シートに類似しており、アラート、などのオプションを提示します:

Image of the alert

示すのないサポートされている方法はありませんそれはiPhoneの場合とまったく同じ方法(下部から)です。あなたがそれについて考えるなら、それを保持する多くの方法からiPadの底に到達することは非常に難しいです。センターははるかに理にかなっています。

0
ここ

方法showInViewshowFromRectはあなたがshowInView方法が、あなたが取得することはできませんあなたがiPadの中で使用するのと同じ機能を使用してcancelオプションを取得しますiPhone上の違いになります。ここで

は、あなたのactionSheet起源をカスタマイズすることができ、いくつかの方法は、画像の下に表示されています。

enter image description here

UIActionSheet's showFromRect:inView:animated:方法でのみiPadに使用されることを想定しています。 iPhoneでの動作は未定義です。

希望すると、これが役立ちます。

+0

@vibhavしかし、私はそれを幅に設定するのは避けていません。それは同じ幅を常に示しています – Muju

+0

上記の幅を設定する必要はありません 'showFromRect'メソッドを使用するか、[これを数分以内に設定することができます](https://www.appcoda.com/uiactionsheet- uipopovercontroller-tutorial /) – vaibhav

+0

@vibhav私はiPhoneと同じように表示する必要があります。 – Muju

1

最初にUIActionSheetが廃止されましたので、最新のコードを使用してください。

- (IBAction)capture:(UIButton *)sender { 

    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleActionSheet]; 
    [alert addAction:[UIAlertAction actionWithTitle:@"Camera" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { 

     // Code for Cam 
    }]]; 
    [alert addAction:[UIAlertAction actionWithTitle:@"Gallery" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { 

     //Code for Gallery 
    }]]; 
    [alert addAction:[UIAlertAction actionWithTitle:@"Remove Photo" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { 

     //Code for removing photo 
    }]]; 
    [alert addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { 

    }]]; 
    [alert setModalPresentationStyle:UIModalPresentationPopover]; 

    UIPopoverPresentationController *popPresenter = [alert popoverPresentationController]; 
    popPresenter.sourceView = sender; 
    popPresenter.sourceRect = sender.bounds; // You can set position of popover 
    [self presentViewController:alert animated:TRUE completion:nil]; 
} 

@balkaran singhが言ったように、ipadのデフォルトの動作です。それを変更することはできません。あなたが望むなら、具体的なカスタムクラスとアニメーションによって、必要なものを正確なUIにすることができます。

関連する問題