点击 UITextField 时弹出 UIPicker [英] Popup UIPicker when tapping on UITextField

查看:38
本文介绍了点击 UITextField 时弹出 UIPicker的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户点击文本字段时,我需要显示/显示一个选择器,我的意思是,它应该显示一个选择器而不是键盘.此外,选择器上方有某种完成"按钮,因此用户点击它,选择器中的值被复制到文本字段,然后选择器再次隐藏.

I need to display/show a Picker when a user taps on a textfield, I mean, it should appear a picker instead of a keyboard. Also, some sort of "done" button above the picker so the user clicks on it and the value from the picker is copied to the textfield and the picker is hidden again.

我从网上查了很多教程,但没有找到能真正帮助我的人.

I've checked many tutorials from the web but haven't found anyone that can really help me.

我找到了一个教程,它为我指明了正确的方向,但我仍然没有在单击文本字段时让键盘消失.使用 UIToolBar 上的完成"按钮关闭 UIPickerView by @slev

I found a tutorial that pointed me in the right direction but I'm still missing to disappear the keyboard when clicking on the textfield. Dismissing UIPickerView with Done button on UIToolBar by @slev

有什么想法吗?

推荐答案

如果您的目标不是早于 3.2 的 iOS 版本,这很容易.您可以将 UIPickerView 指定为 UITextField 的 inputView 属性,将包含完成"按钮的 UIToolbar 指定为 inputAccessoryView,或者您可以制作一个同时具有这两个功能的 UIView选择器和工具栏作为子视图并将其分配为 intputView(并将 inputAccessoryView 保留为 nil).无论哪种方式,当文本字段被激活时,这都会为您的选择器设置动画以代替普通键盘.

If you're not targeting iOS versions older than 3.2, it's easy. You can either assign the UIPickerView as the inputView property of the UITextField and the UIToolbar containing the 'done' button as the inputAccessoryView, or you could make a single UIView that has both the picker and the toolbar as subviews and assign that as the intputView (and leave inputAccessoryView as nil). Either way, this will animate your picker in in place of the normal keyboard when the text field is activated.

文档.

这篇关于点击 UITextField 时弹出 UIPicker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆