iOS如何实现一个下拉列表和如何照顾closures它?

我需要一些关于如何在iOS中实现下拉列表function的input。

我有一些解决scheme就像使用UITableView来显示文本项目列表。 (在我的情况下,列表可以是静态的,也可以是dynamic的,所以UITableView对我来说似乎是个不错的select)。 但是我无法弄清楚的一件事是如何解开下拉菜单

假设在视图的某处打开了这个下拉列表(假设这个视图占据了整个屏幕)。 下拉菜单一旦打开,当我点击视图中的其他位置时,就会被解散(closures),就像典型的下拉菜单在桌面环境中的工作方式一样。 我怎么做?

一种方法是在视图上侦听touchesBegan事件,看看下拉是否打开 – 这是好的,但问题是,如果我有像button的事情,当用户点击其中一个,然后我没有收到touchesBeganinput视图。

我如何去解决这个以通用的方式?

下拉列表通常在iOS中使用UIPickerView实现。 select器视图可以设置为文本字段的input视图,该视图将会保持下拉菜单,然后以与键盘相同的方式在屏幕上和屏幕外animation。

你通常也需要一个UIToolbar作为input附件视图,它包含一个“完成”button,它出现在选取器上方,如果你没有自动做出select,你可以select退出。

您可以通过将resignFirstResponder发送到文本字段来删除选取器,无论是从选取器视图委托方法还是完成button的操作方法。

您创build工具栏作为附件视图,如下所示:

 accessoryView = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; accessoryView.barStyle = UIBarStyleBlackTranslucent; UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneTapped:)]; accessoryView.items = [NSArray arrayWithObjects:space,done, nil]; textField.inputAccessoryView = accessoryView; 

这会给你一个单一的“完成”button连接到一个名为doneTapped的操作方法:

试试这个。 这可能有帮助。

 1) Add the UITableView on a transparent UIView. 2) The UIView should have the same size as the display screen. 3) The UITableView shall take the same small size you have. 4) Implement the touches method as you mentioned for the holding UIView. 

首先,如果你在iPad上,一个UIPopoverViewController正是为此而devise的。 如果你需要更多的定制,我总是有一个隐形的全屏大小的button,在下拉菜单下面取消隐藏。 它覆盖整个屏幕,当被触摸或下拉式解散时隐藏自己。 超级简单。

我发现这个项目在github上有用。 https://github.com/kmdarshan/dropdown

我为iOS创build了一个下拉控件。 你可以从下面的URL检查出来

https://github.com/iVishal/VSDropdown