In my app I have a form where the user fills out his/her information, the form has some textfields and amongst those is one where the user can select their country. This textfiled is a dropdown which when active displays a dropdown (tableview) with the list of countries to select. I have created a demo project, to reproduce the issue and share it here.
In this demo, I have add 3 textfields, when the user goes to the second textfiled, using the next button on the keyboard, a tableview is displayed. Now what I want to achieve is my keyboard should hide when the dropdown tableview is displayed.
I have put in quite some time trying to figure this out but has no success.
Here is the link to the demo project:
https://github.com/tejaskutal/IQKeyboardManagerDemo
Any help is greatly appretiated.
P.S: For some weird reason, when you click next when the first textfiled is active, it jumps to the third one first and then goes to second textfield after next is clicked one more time. However, this has nothing to do with the original problem so I left it there.
No need to handle different delegate. In your viewDidload you can do something like,
yourTextField.inputView = UIView.init(frame: CGRect.zero)
yourTextField.inputAccessoryView = UIView.init(frame: CGRect.zero)
so, for that textfield keyboard or toolbar will not be appeared.
You can use your UITextField's delegate and implement the textFieldShouldBeginEditing. Something like
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
if textField == theTextFieldToIgnore {
view.endEditing(true)
return false
}
}
As for your textField chaining, you should implement the textFieldShouldReturn
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if textField == firstOne {
textField.resignFirstResponder()
secondOne.becomeFirstResponder()
}
return true
}
Return key Action which textfield you want to resign
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[self.yourTf resignFirstResponder];
}
Related
While displaying an alert(Wrong password) in IOS 8, keyboard opens automatically and hide the alert(just in Iphone 4s because of the screen's size), so I can't click in "OK" and I also can't dismiss keyboard because first I need to close the alert!
Keyboard hides alert
(It seems the app is recovering last keyboard's state and showing up again)
How can I close the keyboard before calling the alert?(this way the state will be "closed")
I've tried:
myTextField!.resignFirstResponder()
While calling the button, but it didn't work, the alert shows up and automatically the keyboard opens over it !
if myTextField!.resignFirstResponder() is not working properly try this when you present the alert before call this -->self.view.endEditing(true)
the above function is not work well , try
Choice -1 :Using the Responder Chain
UIApplication.sharedApplication().sendAction("resignFirstResponder", to:nil, from:nil, forEvent:nil)
This will resign the first responder (and dismiss the keyboard) every time, without you needing to send resignFirstResponder to the proper view. No matter what, this will dismiss the keyboard. It’s by far the best way to do it: no worrying about who the first responder is
Choice -2 :UIView’s endEditing
(assuming your text field is a subview of the view you call this on). Most of the time:
self.view.endEditing(true)
set Delegate to myTextField
Include
func textFieldShouldReturn(textField: UITextField) -> Bool
{
textField .resignFirstResponder()
return true
}
Other wise Try the following
var activeField : UITextField!
func textFieldDidBeginEditing(textField: UITextField)
{
activeField = textField
}
func textFieldDidEndEditing(textField: UITextField)
{
activeField = nil
}
func textFieldShouldReturn(textField: UITextField) -> Bool
{
textField .resignFirstResponder()
return true
}
Call activeField.resignFirstResponder() before alert appears
I think from iOS8 you need to use UIAlertController instead of UIAlertView. Using UiAlertView in iOS8 and above is causing keyboard to popup unnecessarily. I have seen this and i made a condition to use UIAlertController for iOS8 and above. In below version UIAlertView should work fine
This is UIAlertView bug on iOS 8.
I have same problem but UIAlertController has not problem. :3
UIAlertView was deprecated since iOS8.
https://developer.apple.com/reference/uikit/uialertview
In Swift 4 : below code worked for me
DispatchQueue.main.async() {
self.view.endEditing(true)
}
I really don't understand why this is happening. Any hints would be much appreciated.
Setup:
UIViewController with the following subviews:
UISearchBar
UITextField
UIView (with PickerView and Done button) - viewDidLoad hides it.
I have two helper functions which can be simplified as:
func showPickerView () {
myPickerSubView.alpha = 1
}
func hidePickerView () {
myPickerSubView.alpha = 0
}
(There's actually animation, sliding up and down etc. but that part works fine)
What I want is for myPickerSubView to show when the person taps on the UISearchBar or the UITextField. myPickerSubView is dismissed by the Done button on myPickerSubview.
I started with the UITextField which I got working perfectly with:
func textFieldDidBeginEditing(textField: UITextField)
{
if !(pickerSubViewVisible)
{
self.showPickerView()
}
eventFloorTextField.endEditing(true)
}
func pickerView(eventFloorPicker: UIPickerView, didSelectRow row:Int, inComponent component: Int {
//.. other processing code
self.eventFloorTextField.text = "My Pick: \(myPick)" //so the textfield gets updated
}
I wanted to use a UISearchBar instead because I wanted to uses the built in scope bar for some added functionality. So having set the UISearchBar delegate to self etc. I thought I could just do:
func searchBarTextDidBeginEditing(searchBar: UISearchBar)
{
eventFloorSearchBar.endEditing(true)
self.showPickerView()
}
When you press on the UISearchBar the first time, the PickerSubView shows up as expected. Then, no matter what I do (press on my Textfield, dismiss the PickerSubView or not,) if I tap on the UISearchBar a second time, a keyboard will come up from the bottom. If my PickerSubView is already present, the keyboard will cover it. If the PickerSubView is not present, if I tap on the UITextField, the PickerSubView will slide up behind the keyboard.
I do not know if it is significant - but the caret (blinking cursor) remains in the UISearchBar text field but even if I make it go away by tapping on the UITextfield first, when I tap UISearchBar a second time, it will always be a keyboard showing up.
I've tried resignFirstResponder as well as various other methods, but none of them seem to work.
I have an app with a UITextField. When the containing view appears, I want a keyboard to slide up and begin editing this text field. Not sure how I would go about doing this?
I've tried:
func viewDidLoad {
textField.editing == true
textField.selected == true
In viewWillAppear add a call to becomeFirstResponder, something like this:
[textField becomeFirstResponder];
or in Swift:
textField.becomeFirstResponder()
I am doing two operations on a textfield:
1) To move the textfield using pan gesture.
2) To write on the textfield when the user clicks on it.
Concern is that I want to perform one operation at a time i.e when the user is writing he cannot scroll and vice versa.
Name of UIButton action --- button
Name of UIButton outlet --- optionButton
- (IBAction)button:(id)sender
{
if([_optionButton.titleLabel.text isEqualToString:#"SCROLL"])
{
NSLog(#"can scroll");
NSLog(#"POINT=============%d ",point);
point=0;
[_optionButton setTitle:#"WRITE" forState:UIControlStateNormal];
textField1.enabled=NO;
}
else
{ NSLog(#"can write");
NSLog(#"POINT=============%d ",point);
point=1;
[_optionButton setTitle:#"SCROLL" forState:UIControlStateNormal];
textField1.enabled=YES;
}
}
The point variable is controlling the complete operation and is set to 1 at the beginning.This means that the user can write in the beginning and if he want to write he has to press the button.
The problem that I am facing is that by using "textField1.enabled=NO" I am not able to scroll the textfield.
Is there any function using which I can remove the focus from the textfield but can scroll it.
You can use this code to remove focus (As said in one of the comment),
[textField1 resignFirstResponder];
you can also remove focus from text field by calling [textField1 endEditing:YES]
An UITextfield only has 1 line, so you can´t scroll it.
UPDATE
If you want to move your UITextfield with your finger use UIPanGestureRecognizer in your parent view and apply the changes in your uitextfield.
You can simply follow the steps:
Implement UITextFieldDelegate into your Controller
class ClassName: UIViewController, UITextFieldDelegate {....}
Implement the "textFieldShouldBeginEditing" function
class ClassName: UIViewController, UITextFieldDelegate {
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
return false
}
}
So returning false in textFieldShouldBeginEditing will simply not allow to edit into the UITextField.
How to handle when uitextview became first responder. I have text in text view and I want when the view became active want to clear the text. How can I do that? Thanks in advance
You can definitely use a UITextViewDelegate method of:
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
Just return YES and intercept inside that method. You can also do it for UITextFields with UITextFieldDelegate and:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
textViewShouldBeginEditing actually triggers before the text view becomes the first responder.
textViewDidBeginEditing will trigger once the text view becomes the first responder and would be the best place to execute code that needs to know what the active textview is.
If you are not really worried about which field is active and just want to clear the text once the field is tapped on you could use either function.
EDIT: The same methods are available for text fields.
As above, you can override becomeFirstResponder but note that you must call the superclass implementation. If you don't, things like popping the keyboard on a text field won't work. i.e.
override func becomeFirstResponder() -> Bool {
if super.becomeFirstResponder() {
// set up the control state
return true
}
return false
}
The Swift 4 solution
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
return true
}
Previous answers do the job great for UITextBox, but if you have a custom class derived from NSResponder and need to know when it becomes first responder:
-(BOOL) becomeFirstResponder
{
// Your stuff here
return YES;
}