I got problem while showing timer picker. There are two text fields: 1. first one opens a number pad. 2. second opens time picker from material controls.
Once I start editing first TF & if I tap on second TF the number pad is not getting dismissed even if I tap on Done button.
the done button is created using ToolBar & the method contains "view.endEditing" code. It works fine normally but not in above condition.
override func viewDidLoad() {
super.viewDidLoad()
rateInPointsTextField.delegate = self
}
extension MyAvailabilityVC : UITextFieldDelegate{
let toolbar = UIToolbar()
toolbar.sizeToFit()
//done button for toolbaar
let done = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(donePressed))
toolbar.setItems([done], animated: false)
self.rateInPointsTextField.inputAccessoryView = toolbar
#objc func donePressed(){
self.view.endEditing(true)
}
func textFieldDidBeginEditing(_ textField: UITextField) {
if textField == self.fromTimeTextField{
let timePicker = MDTimePickerDialog()
timePicker.delegate = self
timePicker.tag = 100
timePicker.clockMode = .mode12H
timePicker.show()
self.view.endEditing(true)
}
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
self.rateInPointsTextField.resignFirstResponder()
return true
}
}
Confirm UITextFieldDelegate in your view controller & set TF delegate to self
class YourViewController: UIViewController, UITextFieldDelegate {
#IBOutlet weak var your_tf: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
your_tf.delegate = self;
}
....
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
}
Now when you will press done/return button of keyboard then keyboard will dismiss. If you want to dismiss from your created button then make call from done #Action fun to textFieldShouldReturn(your_tf) like this.
...
#objc func donePressed(){
textFieldShouldReturn(your_tf)
}
Related
I have a UITextfield in a storyboard.
ClearButton is set to 'is always visible'
searchTextField.addTarget(self, action: #selector(searchTextFieldDidChange(textField:)), for: .editingChanged)
When the text field changes, this method is called
#objc func searchTextFieldDidChange(textField: UITextField){
if textField.text == "" {
textField.resignFirstResponder()
}
fireSearch()
}
When I clear the text field using backspace, textField.resignFirstResponder() is called, the keyboard vanishes as I want it.
When I clear the text field using the clear button, textField.resignFirstResponder() is called, the keyboard vanishes and appears again immediately.
What can I do that the keyboard keeps being closed when I tap the clear button?
Give this a try...
// conform to UITextFieldDelegate
class ViewController: UIViewController, UITextFieldDelegate {
// assuming this is created in Storyboard
#IBOutlet var searchTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
searchTextField.addTarget(self, action: #selector(searchTextFieldDidChange(textField:)), for: .editingChanged)
// set the delegate
searchTextField.delegate = self
}
#objc func searchTextFieldDidChange(textField: UITextField){
if textField.text == "" {
textField.resignFirstResponder()
}
fireSearch()
}
func textFieldShouldClear(_ textField: UITextField) -> Bool {
// clear the text
// Note: this does NOT fire searchTextFieldDidChange()
textField.text = ""
// resign
textField.resignFirstResponder()
// if you want to fire the search after text has been cleared
//fireSearch()
// return false to stop the default action of the clear button
return false
}
func fireSearch() {
print(#function)
}
}
I add a click on uitextfield, the keyboard does not show when touched only with a long press in the field.
What can I do to make the keyboard pop up when I press it and I won't hold it for a long time?
TAP TAP works and deletes other views as well ...
What am I doing wrong?
class DeparturesView: UIViewController {
…
private let searchTextFieldData: UISearchTextField = {
let searchTextFieldData = UISearchTextField()
searchTextFieldData.translatesAutoresizingMaskIntoConstraints = false
searchTextFieldData.keyboardType = .alphabet
searchTextFieldData.borderStyle = .roundedRect
searchTextFieldData.autocorrectionType = .no
searchTextFieldData.spellCheckingType = .no
searchTextFieldData.layer.cornerRadius = 10
searchTextFieldData.backgroundColor = .white
searchTextFieldData.returnKeyType = .search
return searchTextFieldData
}()
}
override func viewDidLoad() {
…
//Mark - Add gesture to app
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
return touch.view == self.view
}
//Mark - Hide keyboard
let hideKeyboards = UITapGestureRecognizer(target: self.view, action: #selector(UIView.endEditing(_:)))
hideKeyboards.delegate = self
view.addGestureRecognizer(hideKeyboards)
//Mark - Detele other view and show keyboard
let hideTableView = UITapGestureRecognizer(target: self, action: #selector(tapHandler))
hideTableView.numberOfTapsRequired = 1
hideTableView.delegate = self
searchTextFieldData.addGestureRecognizer(hideTableView)
}
#objc func tapHandler(_ sender: UITapGestureRecognizer){
print("Show tap tap")
favouriteLabel.removeFromSuperview()
tableViewWithFavouriteBusStop.removeFromSuperview()
}
extension DeparturesView: UITextFieldDelegate{
func textFieldDidBeginEditing(_ textField: UITextField) {
becomeFirstResponder()
}
Follow staps
Remove "hideTableView" TapGesture
add self.earchTextFieldData.delegate = self in viewDidLoad and
replace "textFieldDidBeginEditing"
func textFieldDidBeginEditing(_ textField: UITextField) {
favouriteLabel.removeFromSuperview()
tableViewWithFavouriteBusStop.removeFromSuperview()
becomeFirstResponder()
}
I'm using a UITextField to show results of a calculation but I don't want the keyboard to appear when the user taps on the UITextField.
I'm using a UITextField because I still want the user to be able to Copy and Paste the calculation back into the UITextField, but I don't want the keyboard to show up.
UIKeyboardWillHide only works after the keyboard is displayed.
Swift 4.2, This works for me.
put in viewDidLoad()
//It will Hide Keyboard
textField.inputView = UIView()
//It will Hide Keyboard tool bar
textField.inputAccessoryView = UIView()
//It will Hide the cursor
textField.tintColor = .white
Its quite simple to do with UITextField. Use this code in viewDidLoad()
self.txtresult.inputView = UIView()
self.txtresult.inputAccessoryView = UIView()
For iPads, according to this response of #Awais Jamil, add the following code
textField.inputAssistantItem.leadingBarButtonGroups = []
textField.inputAssistantItem.trailingBarButtonGroups = []
First set delegate with your UITextField in self class.
You can do with below 2 ways.
1. From storyboard
2. From Code ( You can write at viewDidLoad() )
textField.delegate = self
Then declare protocol UITextFieldDelegate in your class.
Now call delegate method.
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
self.view.endEditing(true)
return true
}
textField.inputView = UIView()
This line of code in your textFieldDidBeginEditing func will do the job.
My func:
func textFieldDidBeginEditing(_ textField: UITextField) {
keyboardView.activeTextField = textField
textField.inputView = UIView()
}
You can hide keyboard in UITextFieldDelegate method textFieldDidBeginEditing:(textField: UITextField) like below :
func textFieldDidBeginEditing:(textField: UITextField) {
self.view.endEditing(true)
}
you could use an "empty" inputview like this:
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
let inputView = UIView(frame: .zero)
inputView.backgroundColor = UIColor.clearColor()
inputView.opaque = false
textField.inputView = inputView
return true
}
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
self.view.endEditing(true)
return false
}
You can try this as well VIA text field delegate method.
func textFieldDidBeginEditing(_ textField: UITextField) {
textField.resignFirstResponder()
// your custom impl
}
In my app there is a webView with default website google. I see in some apps on any webpage there is an address bar on top so user can enter new website address any time. How can we do that? Thanks!
Put your web view inside a UINavigationView, then set its headerView to be a UITextField. Something like this:
class WebsiteViewController: UIViewController, UITextFieldDelegate {
var addressField: UITextField? = nil
override public func viewDidLoad() {
super.viewDidLoad()
addressField = ({
let field = UITextField()
field.autocorrectionType = UITextAutocorrectionType.No
field.delegate = self
field.addTarget(self, action: "addressChanged:", forControlEvents: .EditingChanged)
field.addTarget(self, action: "addressEditEnded:", forControlEvents: .EditingDidEnd)
field.sizeToFit()
return field
})()
}
override public func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.navigationItem.titleView = addressField
}
#IBAction func addressChanged(sender: UITextField) {
if let newString = addressField?.text where newString != searchString && newString.characters.count != 1 {
// do any filtering you might want to
}
}
#IBAction func addressEditEnded(sender: UITextField) {
// trigger the page load
}
}
That should cover the essentials.
I have a UIViewController with a UITextField in it and I'm trying to dismiss the keyboard when I click away or the view is dismissed. However, when I call resignFirstResponder(), the keyboard still doesn't dismiss and I'm not quite sure why. Here's my code:
class MessageViewController: UIViewController, UITextFieldDelegate {
var messageTextField : UITextField = UITextField()
override func viewDidLoad() {
...
messageTextField.frame = CGRectMake(10, UIScreen.mainScreen().bounds.size.height-50, UIScreen.mainScreen().bounds.size.width-80, 40)
messageTextField.delegate = self
messageTextField.borderStyle = UITextBorderStyle.Line
messageTextField.becomeFirstResponder()
self.view.addSubview(messageTextField)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil)
...
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
self.view.endEditing(true)
println("Touched")
}
func keyboardWillShow(notification: NSNotification) {
var keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey]as? NSValue)?.CGRectValue()
let messageFrame = messageTextField.frame
let newY = messageFrame.origin.y - keyboardSize!.height
messageTextField.frame = CGRectMake(messageFrame.origin.x, newY, messageFrame.size.width, messageFrame.size.height)
}
func keyboardWillHide(notification: NSNotification) {
var keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey]as? NSValue)?.CGRectValue()
let messageFrame = messageTextField.frame
let newY = messageFrame.origin.y - keyboardSize!.height
messageTextField.frame = CGRectMake(messageFrame.origin.x, newY, messageFrame.size.width, messageFrame.size.height)
}
}
Does anyone know why the keyboard isn't dismissing? I added the UITextField to the view programmatically as opposed to using storyboard. Does that make a difference?
class ViewController: UIViewController,UITextFieldDelegate {
confirm protocols
messageTextField.delegate=self
set delegate
func textFieldShouldReturn(textField: UITextField!) -> Bool
{
messageTextField.resignFirstResponder()
return true;
}
Use this code..
The following is how I did for that problem. I hope this method solve your problem.
textfield
#IBOutlet var userID : UITextField!
function.
func textFieldShouldReturn(textField: UITextField!)-> Bool
{ userID.resignFirstResponder( )
return true
}
In your desired function, you need to write this syntax.
#IBAction func login(sender: AnyObject)
{
userID.resignFirstResponder( )
}
This is in ViewDidLoad or ViewDidAppear
override func viewDidLoad( )
{
userID.delegate = self;
}
I cannot comment on the previous user. That's why I write this one.
I hope this gives you idea.
func textFieldShouldReturn(textField: UITextField) -> Bool {
self.view.endEditing(true)
//textField.resignFirstResponder()
return false
}
check delegate add into your viewDidLoad()
self.yourTextField.delegate = self;
how-to-dismiss-uitextfields-keyboard-in-your-swift-app
This might helps you :)
#vijeesh's answer will probably work, and the logic is almost correct, but it is technically wrong if you ever use more than one UITextField. textField is the UITextField parameter that is passed when textFieldShouldReturn is called. The problem is, you're just declaring:
textField.resignFirstResponder()
Your program doesn't know what UITextField you're referring to. Even though you may only have one textField in your program, and you know that it's your messageTextField, the program still doesn't know that. It just sees "textField". So you have have to tell it what to do for each UITextField in your program. Even if you have just one. This should work:
func textFieldShouldReturn(textField: UITextField) -> Bool {
if textField == messageTextField {
messageTextField.resignFirstResponder()
}
return true
}
very simple.
first you add Tap Gesture
var tap = UITapGestureRecognizer(target: self, action: "handle:")
view.addGestureRecognizer(tap)
in function handle
func handle(tap: UITapGestureRecognizer){
view.endEditing(true)
}
so you can dismiss keyboard when you click outside UITextField