ios - 下拉样式UIPickerView?

标签 ios swift drop-down-menu uipickerview

我想知道如何让 UIPickerView 在点击下拉样式按钮后从屏幕底部向上滑动。如下图所示:

CloudConvert screenshot of a drop-down style UIPickerView

我经常在我经常使用的应用程序中遇到这种类型的选择器 View ,所以老实说,我希望通过设置 UIPickerView 轻松找到这种选择器 View style 属性,或类似的东西。这甚至是 UIPickerView 还是我必须手动创建这种 View ?

最佳答案

这样做的一种方法是拥有一个普通的 UITextField,然后指定一个 UIPickerView 作为该文本字段的 inputView。这样,当您点击文本字段时,键盘不会出现,而是您会看到选择器 View 。

例子

首先声明一个普通的UIPickerView实例:

let yourPicker = UIPickerView()

UITextField 的导出:

@IBOutlet weak var yourTextField: UITextField!

在您的 viewDidLoad 中,您将元素连接在一起

yourPicker.delegate = self
yourPicker.dataSource = self
yourTextField.inputView = yourPicker

然后需要实现UIPickerViewDelegateUIPickerViewDataSource的方法

最后。在 pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) 中更新文本字段的值:

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
   yourTextField.text = yourDataArrayUsedInThePicker[row]
}

阅读更多

输入 View 的描述 https://developer.apple.com/library/content/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/InputViews/InputViews.html

一种比我更好的解释: Show UIPickerView text field is selected, then hide after selected

希望对你有帮助。

关于ios - 下拉样式UIPickerView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40374489/

相关文章:

ios - 根据出生日期计算年龄

ios - 分发配置文件错误 : Valid signing identity not found

ios - 如何在 iOS Objective C 中将 NSData 转换为 UInt8 数组?

javascript - 使用 ng-option 提交对象

ios - React Native - secureTextEntry 禁用 IOS 13+ 强密码行为

android - Jwplayer:在 Android Chrome 和 iOs Safari 上设置自动启动

ios - 在后台更新位置

ios - 通过键盘向上滚动后,TextView 没有移回原始位置

javascript - 下拉列表可以输入新值而不是列表

php - 选择元素未在表单提交上发布数据