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!

5
  • 1
    UIButton doesn't have an inputView property. What exactly do you want to do?
    – rounak
    Commented Jul 11, 2015 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
    Commented Jul 11, 2015 at 20:52
  • You can create a view for the picker off screen view and move it on screen when you need it.
    – Jason
    Commented Jul 11, 2015 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
    Commented Jul 11, 2015 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
    Commented Oct 8, 2015 at 2:05

4 Answers 4

12

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
    }

}
2
  • How to use this in code? I tried this "myButton.inputView = myPicker"
    – Bhanuteja
    Commented Sep 18, 2019 at 9: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
    Commented Sep 18, 2019 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

1
  • 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
    Commented Nov 23, 2021 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.

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.