15

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:

@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:

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!

Chase
  • 453
  • 1
  • 5
  • 15
  • 1
    UIButton doesn't have an inputView property. What exactly do you want to do? – rounak Jul 11 '15 at 20:49
  • 1
    I want a button to trigger a popup UIDatePicker so I can get a date variable without cluttering the UI. – Chase Jul 11 '15 at 20:52
  • You can create a view for the picker off screen view and move it on screen when you need it. – Jason Jul 11 '15 at 21:19
  • 1
    That is definitely an option, but I would really love to use inputView or something like it if I can. – Chase Jul 11 '15 at 21:31
  • InputView is a better option. I had the popup method before, but uitableView will not auto adjust when it appears. – Ace Green Oct 08 '15 at 02:05

4 Answers4

11

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:

class MyButton: UIButton {

    var myView: UIView? = UIView()
    var toolBarView: UIView? = UIView()
    
    override var inputView: UIView? {
        get {
            myView
        }
        
        set {
            myView = newValue
            becomeFirstResponder()
        }
    }

    override var inputAccessoryView: UIView? {
        get {
            toolBarView
        }
        set {
            toolBarView = newValue
        }
    }
    
    override var canBecomeFirstResponder: Bool {
       true
    }

}
Ilias Karim
  • 4,798
  • 3
  • 38
  • 60
Brian
  • 924
  • 13
  • 22
  • How to use this in code? I tried this "myButton.inputView = myPicker" – Bhanuteja Sep 18 '19 at 09:20
  • I added a tap gesture recogniser to the button instance. When the button is tapped I set the delegate for the picker. Then set a `myButton.inputAccessoryView` to a `UIToolbar` and `myButton.InputView = myPicker`. If you want to set a few attributes for the toolbar maybe create a separate function which will return a `UIToolbar`. – Brian Sep 18 '19 at 17:44
4
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

Frits
  • 7,341
  • 10
  • 42
  • 60
MikeNatty
  • 147
  • 1
  • 5
  • The whole point of op's question is that he does not want to use a `UITextField()` inputView, and don't know how to use the inputView property of a UIButton because it is `get-only` so this answer feels a little bit off-topic. Thank for participating anyway – itMaxence Nov 23 '21 at 14:02
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.

Tip: set tintColor of the UITextField to UIColor.clearColor() to hide the cursor.

Paludis
  • 714
  • 6
  • 21
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.

Community
  • 1
  • 1
Jason
  • 650
  • 2
  • 10
  • 33