I'm doing custom keyboard thing on Xcode using swift.
My problem is on KeyboardViewController.swift file.
I have no idea how to use next keyboard button :e
wanted to connect #IBOutlet var nextKeyboardButton: UIButton! to the button that i created but it is not working ..
When your "Next Keyboard" button tapped, call
advanceToNextInputMode()
in your KeyboardViewController.
I am using this function to achieve this but with the iphone keyboard. Maybe it could help
func textFieldShouldReturn(textField: UITextField) -> Bool {
let nextTage=textField.tag+1;
// Try to find next responder
let nextResponder=textField.superview?.viewWithTag(nextTage) as UIResponder!
if (nextResponder != nil){
// Found next responder, so set it.
nextResponder?.becomeFirstResponder()
}
else
{
// Not found, so remove keyboard
textField.resignFirstResponder()
}
return false // We do not want UITextField to insert line-breaks.
}
Related
I create sign in page with swift programmatically coding. I tried simulator and device but keyboard did not opened when clicked on textfied.
when I write code in viewController with stroyboard, keyboard had opened. But, without using storyboard, keyboard is not opening.
Did you set the TextField's delegate? In the Storyboard you must have, but in code that is sometimes forgotten.
class ViewController: UIViewController, UITextFieldDelegate {
let textField = UITextField()
func viewDidLoad() {
super.viewDidLoad()
textfield.delegate = self // Did you do this?
}
}
If you are using simulator then you must go to I/O -> Keyboard -> Toggle software keyboard.
If you are using physical device then through code you need to set as as first responser as below:
textfieldName.becomeFirstResponder()
I'm creating an app and I need to login to it and I don't want to use 2 Text Field for insert Email and Password. What I would wish to achieve is: click on the button "Log in", then it will open the classic full screen keyboard that is usually appearing when you are doing click on UITextField in tvOS.
Can someone please write an example of code on how to invoke the keyboard clicking on a UIButton instead of UITextField? Thanks a lot!
While your UI is questionable, once your first text field finishes editing, you could start the other one.
Set the first text field UITextFieldDelegate to the view controller, and then:
class ViewController: UIViewController , UITextFieldDelegate{
#IBOutlet weak var tf2: UITextField!
#IBOutlet weak var tf1: UITextField!
#IBAction func click() {
tf1.becomeFirstResponder()
}
func textFieldDidEndEditing(_ textField: UITextField) {
if (textField == tf1){
tf2.becomeFirstResponder()
}
}
}
If you want a UITextField to open up the keyboard without the user having to tap on it, you can use yourTextField.becomeFirstResponder().
I don't think you can present the keyboard without any associated input view
I searched everywhere and can't seem to find anything on this. For example I want to have more then one UITextField on the same screen. If I tap on one and type in information and click return it does an action. If you tap in the second one and return the keyboard it does a different action.
Any suggestions or help would be awesome thanks!
One option is to use tags. In the example below you can select the next textfield, and only on the last one do we login. You could use cases to do whatever you'd like
//automatically move to the next field
func textFieldShouldReturn(_ textField: UITextField) -> Bool
{
let next = textField.tag + 1
// Find next responder
if let nextField = view.viewWithTag(next) as? UITextField {
nextField.becomeFirstResponder()
} else {
// Not found, so remove keyboard.
textField.resignFirstResponder()
//do the final action to login
loginAction()
}
return false
}
Since iOS 9, you can connect the “Primary Action Triggered” event of your UITextField to an action method. So you can connect each text field's “Primary Action Triggered” event to a separate action method to give the text fields different actions.
See this answer for a demo: https://stackoverflow.com/a/26288292/77567
I realized I was really over thinking it.. Took another look and saw what I was trying to do was a lot easier then I thought.. Thanks everybody who helped! (just an example below)
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if field1.resignFirstResponder() {
print("yellow")
}
if field2.resignFirstResponder() {
print("blue")
}
return (true)
}
I have two textFields and a Done button in my VC but I'm having some problems with the ending of editing.
My textFieldDidEndEditing method is called when I tap on one textField after being inside the other one, or when I tap outside the textField (because I added a tap recognizer to the parent view) but not when I tap the Done button.
And, most important (especially when I run on an actual device), the keyboard won't disappear under any of these circumstances, even though my textFieldDidEndEditing method calls resignFirstResponder().
Why isn't the keyboard dismissing? Also, is there a way to have textFieldDidEndEditing get called when I tap outside the field just automatically (without having it come from the tap recognizer)? It just seems like this should be how it works, but if I'm wrong, I'm wrong.
Here's some pertinent parts of my code.
1.Trying to dismiss the keyboard. The first part of this method works, and the value is stored (when the method is called at all, that is). At no point does the cursor disappear from the textField, nor does the keyboard get dismissed.
func textFieldDidEndEditing(_ textField: UITextField) {
if let playerName = textField.text, let playerNum = nameFields.index(of: textField) {
playerNames[playerNum] = playerName
}
resignFirstResponder()
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textFieldDidEndEditing(textField)
return true
}
Also, here's a curious thing: when I set a breakpoint in textFieldDidEndEditing and debug, enter a value in one field and hit Done, it segues to the next scene, and then stops at textFieldDidEndEditing, which at this point has no effect (the values may be stored but they aren't reflected in the new scene).
2.Trying to add the tap recognizer to the done button. I don't have an outlet to the done button in my code, just out of laziness, so that's probably the best solution. But, I'm still interested in why this doesn't work. This is identical to the code that defines the tap recognizer that's working in the parent view.
func dismiss(_ sender:UITapGestureRecognizer) {
nameFields.forEach { textFieldDidEndEditing($0) }
}
override func viewDidAppear(_ animated: Bool) {
for view in view.subviews where view is UIButton {
let dismissTextField = UITapGestureRecognizer(target: self, action: #selector(dismiss(_:)))
dismissTextField.numberOfTapsRequired = 1
view.addGestureRecognizer(dismissTextField)
}
}
You need to call resignFirstResponder inside textFieldShouldReturn method instead of calling textFieldDidEndEditing.
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
Also in your TapGesture method simply call endEditing(_:) with your view instead of looping through the array of textFields.
func dismiss(_ sender:UITapGestureRecognizer) {
self.view.endEditing(true)
}
Swift 5: This solution below is much easier actually.
Simply subclass your ViewController with the text fields to UITextFieldDelegate like this:
class CreateGroupViewController: UIViewController, UITextFieldDelegate {
then if you named your UITextFields: nameTextField & occupationTextField then add the following delegations to your viewDidLoad() method:
self.nameTextField.delegate = self
self.occupationTextField.delegate = self
Then simply add the generic function to your file:
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
If you do it this way, then for every textField reference outlet you create, all of them will hide the keyboard after the user hits the return button no matter which textField he's typing. For each textField you add to your view, add another self.nextTextField.delegate = self line to your viewDidLoad.
Remember to test this with your iPhone/iDevice plugged into your developer computer bc XCode's simulator doesn't bring up the keyboard (bc you're normally testing using a full keyboard). Or if you've set up your testing hardware (iPhone) via WiFi you can do it that way also. Otherwise, your users will be "testing" this on TestFlight.
(So far) I have two view controllers which both have at least one textfield.
When the user taps into a textfield the keyboard pops up.
I have all of the code in place to move content up and then back down again when this happens, (everything is inside of a scroll view which I am led to believe is best practice)
I also have the code in place to dismiss the keyboard when the user taps outside of the textfield.
On the first view controller it works great, but on the 2nd I have a UIPickerView that takes up a good amount of space underneath the textfield. So what happens is when the user goes to tap the most obvious amount of space he/she is actually tapping the scroll view and nothing happens. But if the user taps in a very small area that is empty and not the scroll view the keyboard dismisses.
How can I dismiss the keyboard with the UIPickerView in the way?
Here is an image of what my situation looks like
Here is some of the code
func textFieldShouldClear(_ textField: UITextField) -> Bool {
self.view.endEditing(true)
return false
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
self.view.endEditing(true)
return false
}
func dismissKeyboard() {
view.endEditing(true)
}
You can disable userInteractionEnabled on the picker when the keyboard is shown (or textField become first responder) and enable it back when the keyboard is dismissed (or textField resign first responder).
You have to resignFirstResponder .
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
self.view.endEditing(true)
textField.resignFirstResponder()
return false
}