我是 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
出现在中间(见下图),并且不显示取消按钮:
在 iPhone 上,它看起来像这样:
最佳答案
首先,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/