Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display UIActionSheet on the Bottom of ipad same as iphone in objective c

I am new to iOS and I am facing a problem regarding UIActionSheet.

My code is like this

-(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];
}

This works perfectly on iPhone but when I run this on an iPad, the UIActionSheet appears in the middle (see image below) and also doesn't show a cancel button:

enter image description here

On iPhone, it appears like this:

enter image description here

like image 505
Muju Avatar asked Dec 01 '25 06:12

Muju


1 Answers

At first UIActionSheet is now deprecated so use latest code for it.

- (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];
}

And as @balkaran singh said it's a default behaviour for ipad . you can not change it. If you want then you concrete your custom class and by animation you can get exact UI what you need.

like image 85
VRAwesome Avatar answered Dec 03 '25 20:12

VRAwesome



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!