AssistoLab / DropDown

A Material Design drop down for iOS
MIT License
2.44k stars 622 forks source link

Dismiss Keyboard? #36

Open craigpearce5 opened 8 years ago

craigpearce5 commented 8 years ago

Is there anyway to prevent the keyboard from showing up? If I'm giving the user a list of options, the keyboard is likely not needed.

I've tried textField.resignFirstResponder() but that doesn't work. Also tried some combinations with func textFieldDidBeginEditing(textField: UITextField) etc.

One suggestion would be to extend the dropdown functionality to UILabel. Any other suggestions appreciated.

kevin-hirsch commented 8 years ago

Hello @craigpearce5,

How are you using the DropDown, can you show some code? I think this question is not really related to the use of the DropDown itself, a textField.resignFirstResponder() should be enough, or a view.endEditing() where view is your UIViewController's view.

craigpearce5 commented 8 years ago
var programTextField: UITextField = UITextField()

// user taps on the text field and func is called
func textFieldDidBeginEditing(programTextField: UITextField) {
    showAvailablePrograms()
}  

let programDropDown = DropDown()  

func showAvailablePrograms(){
    programDropDown.anchorView = programTextField
    programDropDown.dataSource = self.passedProgramNameArray!
    programDropDown.show()
    programDropDown.selectionAction = { [unowned self] (index: Int, item: String) in
        self.programTextField.text = item
        self.programIndexPosition = index

    }
}

I've put programTextField.resignFirstResponder() in both textFieldDidBeginEditing and also in textFieldShouldBeginEditing.

kevin-hirsch commented 8 years ago

Wouldn't it be simpler to use a UIButton instead of a UITextField if you just want the dropDown and no possibility to edit textField ? You would just need to add an action to this button to show the DropDown. If you really need to stick with a UITextField though, you could use the delegate function textFieldShouldBeginEditing(_:) like this:

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
   showAvailablePrograms()
   return false // Prohibits the text field to become first responder
}

Does that fixes you problem?

craigpearce5 commented 8 years ago

I wanted the textField to do both, on tap show the drop down and hold the value, simply to reduce the number of elements on screen (1 textfield vs 1 button 1 textfield). It's not the worst thing if I have to add a button. Once I get home I'll try your suggestion.

Thanks - a great tool by the way. Super easy to implement and looks great.

nhnam commented 8 years ago

@craigpearce5 Have you set self to programTextField.delegate ?. And check again if your viewcontroller have inherited from UITextfieldDelegate protocol

craigpearce5 commented 8 years ago

@nhnam Yes... here's a bit more code to show what I'm doing. I didn't get a chance to try @kevin-hirsch suggestions yet though.

class ChildViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate {
        func textFieldDidBeginEditing(programTextField: UITextField) {
             showAvailablePrograms()
        }   

    let programDropDown = DropDown()
    func showAvailablePrograms(){
        programDropDown.anchorView = programTextField
        programDropDown.dataSource = self.passedProgramNameArray!
        programDropDown.show()
        programDropDown.selectionAction = { [unowned self] (index: Int, item: String) in
            self.programTextField.text = item
            self.programIndexPosition = index

    }
 }
}

App delegate includes DropDown.startListeningToKeyboard()