我该如何为iPhone创建自定义弹出窗口?

9
我想为iPhone创建一个自定义弹出窗口,应该长这样: enter image description here 在iPhone和iPod设备上实现这个功能的最正确方法是什么?

2
我认为iOS的哲学是使用操作表而不是弹出窗口(仅适用于iPhone而非iPad)。据我所知,iPhone上没有内置的弹出窗口。 - Fonix
@Fonix 如果是 iPhone 的话你说得对,但是对于 iPad 我们可以使用 UIPopoverView 来实现这个功能。 - Suryakant Sharma
是的,我确实在我的评论中明确提到了那个问题 :P - Fonix
5个回答

11

最好的方法是使用UIActionSheet。 代码在这里:

UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@"Share" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Facebook",@"Twitter", nil];
[actionSheet showInView:self.view];

UIActionSheet会从底部向上弹出,显示一定数量的按钮。您还可以添加一个取消按钮,以自动关闭UIActionSheet。

这是它的样子:

enter image description here


谢谢!但是你忘了在类头中添加<UIActionSheetDelegate>,所以你不会收到警告:'Sending ##viewController *const__strong' to parameter of incompatible type 'id<UIActionSheetDelegate>' - Aviram Netanel
UIActionSheet现已被弃用,请立即使用UIAlertController。请参见https://dev59.com/32445IYBdhLWcg3wM3bx#32295641。 - Suragch

8

6

使用第三方控件,实现此操作。

这里是您可以下载该项目的链接。


5
有许多方法可供选择。我个人会使用以下代码:

有许多方法可供选择。我个人会使用以下代码:

// create view popup
UIView *viewPopup = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
[self.view addSubview:viewPopup];

// create Image View with image back (your blue cloud)
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, width, height)];
UIImage *image =  [UIImage imageNamed:[NSString stringWithFormat:@"myImage.png"]];
[imageView setImage:image];
[viewPopup addSubview:imageView];

// create button into viewPopup
UIButton *buttonTakePhoto = [[UIButton alloc] initWithFrame:CGRectMake(x, y, width, height)];
[viewPopup addSubview: buttonTakePhoto];
[buttonTakePhoto setTitle:@"Take Photo" forState:UIControlStateNormal];
[buttonTakePhoto addTarget:self action:@selector(actionTakePhoto) forControlEvents:UIControlEventTouchDown];
[viewPopup addSubview:buttonTakePhoto];

当点击相机图标时,您可以为Alpha viewPopup添加动画效果,如启用/禁用viewPopup。请注意保留HTML标记。

1
实现您所要求的最简单方法是创建一个视图,该视图看起来符合您的要求并包含适当的UIButtons,然后在按下相机按钮时将其添加为子视图(并以您想要的方式进行动画处理)。尽管Fonix在您的问题评论中的建议是正确的,即在iOS上设计用于此目的的操作表。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接