objective-c - 如何在ipad底部显示与objective c中的iphone相同的UIActionSheet

标签 objective-c uiactionsheet

我是 iOS 新手,我遇到了有关 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 出现在中间(见下图),并且不显示取消按钮:

enter image description here

在 iPhone 上,它看起来像这样:

enter image description here

最佳答案

首先,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。

关于objective-c - 如何在ipad底部显示与objective c中的iphone相同的UIActionSheet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39718152/

相关文章:

objective-c - 如何杀死所有不是主线程的线程?

iOS - 检测静默模式

ios - 是否有某种 UIAccessibility 提示音?

objective-c - (iOS/Objective-C)有条件向UIActionSheet添加不同按钮的更好方法吗?

ios - 为什么 clickedButtonAtIndex 方法甚至在 UIActionSheet 的外侧点击都被调用?

ios - 来自不同上下文的对象,即使它们不是

iphone - 在 UIToolbar 上镜像局部图像

objective-c - 如何从 iOS 中的 xml 中提取文档流?

ios - 调用 InApp 邮件命令时应用程序崩溃

ios - 在 iPad 的 UIActionSheet 中添加一个 UIPickerView