I am attempting to have a UIDatePicker come up as a keyboard when the user hits a UIButton. I was able to get it to work with a textfield, but I don't like how the cursor is visible and the user could enter in any text if they had an external keyboard. Here is my code:
当用户点击UIButton时,我试图让UIDatePicker成为键盘。我能够使用文本字段,但我不喜欢光标是如何可见的,如果用户有外部键盘,用户可以输入任何文本。这是我的代码:
@IBAction func dateFieldStart(sender: UITextField) {
var datePickerStartView : UIDatePicker = UIDatePicker()
datePickerStartView.datePickerMode = UIDatePickerMode.Time
sender.inputView = datePickerStartView // error when sender is UIButton
}
I tried changing the sender to UIButton but it gave this error on the line that is marked above:
我尝试将发件人更改为UIButton,但它在上面标记的行上给出了此错误:
Cannot assign to 'inputView' in 'sender'
I have tried researching it and no one else seems to have had a problem with it. Anyone know how to trigger a UIDatePicker inputView using a UIButton or anything that might work better that the user cannot type into? Thanks!
我试过研究它,似乎没有其他人有问题。任何人都知道如何使用UIButton触发UIDatePicker inputView或任何可能更好用户无法输入的东西?谢谢!
4 个解决方案
#1
3
I wanted to do the same thing, I ended up just overlaying a UITextField over the button and using the inputView of that instead.
我想做同样的事情,我最后只是在按钮上覆盖UITextField并使用inputView。
Tip: set tintColor
of the UITextField to UIColor.clearColor()
to hide the cursor.
提示:将UITextField的tintColor设置为UIColor.clearColor()以隐藏光标。
#2
3
let tempInput = UITextField( frame:CGRect.zero )
tempInput.inputView = self.myPickerView // Your picker
self.view.addSubview( tempInput )
tempInput.becomeFirstResponder()
It's a good idea to keep a reference to tempInput
so you can clean-up on close
保持对tempInput的引用是个好主意,这样你就可以在关闭时进行清理
#3
1
This is years after the original question, but for anyone who may be looking for solution to this you can subclass UIButton and provide a getter and setter for the inputView property. Be sure to call becomeFirstResponder in the setter and override canBecomeFirstResponder. For example:
这是原始问题之后的几年,但对于任何可能正在寻找解决方案的人来说,您可以继承UIButton并为inputView属性提供getter和setter。一定要在setter中调用becomeFirstResponder并覆盖canBecomeFirstResponder。例如:
class MyButton: UIButton {
var myView = UIView()
var toolBarView = UIView()
override var inputView: UIView {
get {
return self.myView
}
set {
self.myView = newValue
self.becomeFirstResponder()
}
}
override var inputAccessoryView: UIView {
get {
return self.toolBarView
}
set {
self.toolBarView = newValue
}
}
override var canBecomeFirstResponder: Bool {
return true
}
}
#4
0
You can create a view for the picker off screen view and move it on screen when you need it. Here's another post on this.
您可以为选择器关闭屏幕视图创建视图,并在需要时将其移动到屏幕上。这是另一篇文章。
#1
3
I wanted to do the same thing, I ended up just overlaying a UITextField over the button and using the inputView of that instead.
我想做同样的事情,我最后只是在按钮上覆盖UITextField并使用inputView。
Tip: set tintColor
of the UITextField to UIColor.clearColor()
to hide the cursor.
提示:将UITextField的tintColor设置为UIColor.clearColor()以隐藏光标。
#2
3
let tempInput = UITextField( frame:CGRect.zero )
tempInput.inputView = self.myPickerView // Your picker
self.view.addSubview( tempInput )
tempInput.becomeFirstResponder()
It's a good idea to keep a reference to tempInput
so you can clean-up on close
保持对tempInput的引用是个好主意,这样你就可以在关闭时进行清理
#3
1
This is years after the original question, but for anyone who may be looking for solution to this you can subclass UIButton and provide a getter and setter for the inputView property. Be sure to call becomeFirstResponder in the setter and override canBecomeFirstResponder. For example:
这是原始问题之后的几年,但对于任何可能正在寻找解决方案的人来说,您可以继承UIButton并为inputView属性提供getter和setter。一定要在setter中调用becomeFirstResponder并覆盖canBecomeFirstResponder。例如:
class MyButton: UIButton {
var myView = UIView()
var toolBarView = UIView()
override var inputView: UIView {
get {
return self.myView
}
set {
self.myView = newValue
self.becomeFirstResponder()
}
}
override var inputAccessoryView: UIView {
get {
return self.toolBarView
}
set {
self.toolBarView = newValue
}
}
override var canBecomeFirstResponder: Bool {
return true
}
}
#4
0
You can create a view for the picker off screen view and move it on screen when you need it. Here's another post on this.
您可以为选择器关闭屏幕视图创建视图,并在需要时将其移动到屏幕上。这是另一篇文章。