How to always hide keyboard but still can select textField value? - ios

I want my UITextField doesn't show keyboard but the value still can be selected (for example, user wants to move cursor to any index in UITextField)
I have tried to do it this way:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self numberInput].delegate = self;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{
[[self numberInput] resignFirstResponder];
}
but the result is, I can't select value from textField. Is there any workaround for this?

You can change the input of the textField.
If you pass an empty UIView, the keyboard will not appear and you should be able to select the text anyway. In swift 2.0:
textField.inputView = UIView()

Related

iOS: Disable keyboard when begin editing TextField

How can I disable the keyboard when I touch-up inside a UITextField?
What I want to do is show a custom digital keyboard instead of the default one.
If I understand correctly that you are looking to create a custom keyboard in the app, I don't think we need to disable the default keyboard when we touch-up inside a UITextField.
We just need to create a custom view and assign it to the inputView property of the UITextField to replace the default keyboard.
For example, something like this:
yourTextField.inputView = yourCustomKeyboardView
See more here.
Note: For Objective-C (Xcode)
In your viewDidLoad: set delegate for textfields which you want to disable.
self.textfield.delegate = self;
and insert this delegate function:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if (textField == yourTextfiledOutletInstance) {
[self showCustomkeyboard];
return NO;
}
return YES;
}
//Show custom keyboard
-(void)showCustomkeyboard{
// Handle your operation here to show custom keyboard
}
set the UITextField delegate in the ViewController class, and then add this method in the class
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return NO;
}
Use resignFirstResponder to dismiss your keyboard.
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return NO;
}
set inputView on the UITextView to the custom view you want to be used in place of the system keyboard.
myTextView.inputView = myCustomView;

iOS toggle default keyboard from ABC mode to 123 mode via code?

I can see how to set the keyboard's overall type via:
self.myTextView.keyboardType = UIKeyboardTypeDefault;
How can I toggle the default keyboard's mode from ABC to 123 and back again via code? Basically the moment the user taps the # character (the # symbol is available when they're in 123 mode) I want to switch their keyboard back to ABC mode.
Any ideas?
You might be able to accomplish this in by using the UITextViewDelegate. It allows you to intercept the keys as they are pressed in the UITextView. This will allow you to switch keyboards when the user presses a certain key. In order to revert back to the default state of the UIKeyboardTypeDefault keyboard, you'll need to change the keyboard type to another, then back to the default.
In your ViewController.h file, make it implement the UITextViewDelegate protocol:
#interface ViewController : UIViewController <UITextViewDelegate>
In your ViewController's viewDidLoad method in the ViewController.m, set the textField's delegate to the view controller:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.myTextView.delegate = self;
}
Finally, we need to capture the key as it is being entered and change the keyboard appropriately. We do this in the shouldChangeCharactersInRange: method.
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if( [#"#" isEqualToString:text] )
{
NSLog( #"Toggling keyboard type");
textView.inputView = nil;
[textView resignFirstResponder];
[textView setKeyboardType:UIKeyboardTypeEmailAddress];
[textView becomeFirstResponder];
[textView reloadInputViews];
textView.inputView = nil;
[textView resignFirstResponder];
[textView setKeyboardType:UIKeyboardTypeDefault];
[textView becomeFirstResponder];
[textView reloadInputViews];
}
return YES;
}
So I was originally confused and thought you actually wanted to change the keyboard type. Now that I'm reading your comment, I realize that you're actually wanting to reset the keyboard to it's default state of showing the letters, instead of numbers and special characters. The above code now does that.

Clear the content of the UITextField's text property for tap

I have a UITextField called place, it's text property contains an NSString. I would like to clear the existing content from the text property when the user taps into the text field.
I tried the code above, but nothing happened. I also tried it with the placeholder property, but it was the same. Do you have any idea what could be the problem? I think it should work.
- (void)textFieldDidBeginEditing:(UITextField *)textField {
self.place.text = nil;
}
2, version - in this case nothing appears in the text field from the beginning
- (void)viewDidLoad
{
[super viewDidLoad];
...
[self textFieldDidBeginEditing:place];
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
textField.text = nil;
}
There is a property clearsOnBeginEditing you can set to YES programmatically or there is a checkbox in Interface Builder.
If you also want to clear your placeholder
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
textField.placeholder = nil;
}
Double check that the UITextField self.place has it's delegate set to your view controller and validate that textFieldDidBeginEditing is being called.

How to disable keyboard appearing when hitting on a text field , iOS?

I have a text field , and i need when the user presses it to show a custom picker.
The picker is shown fine , but the problem is that the keyboard appears on the bottom and i dont want that.
This is an iPad project which i am trying to convert from my iphone one. On the iPhone , this works well and the keyboard is always hidden.
What could i be missing/forgetting to do here ?
EDIT
For future reference what actually happened here , was that in fact both times (iphone & ipad) the keyboard was not hidden. I just thought that it was hidden in the iphone because my picker , which was popping from the bottom was hiding the keyboard as it was on top of it. But on ipad this wasnt the case.
Anyway i fixed it , using the delegate method suggested below.
Caution , i accepted this answer cause it was the one answering specifically what i wanted. The rest of the answers are correct and my considered better for other implementations.
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
// Here You can do additional code or task instead of writing with keyboard
return NO;
}
this delegate method will get called first when you hit to textfield and if you write NO as a boolean value means you dont want to begin editing so it will not present Keyboard.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if(textfield == yourtextField)
{
[textfield resignFirstResponder];
// Show you custom picker here....
return NO;
}
}
and you need to implement the uitextfielddelegate in the controller.
and give assign the delegate to yourtextField.
Use textfield delegate.
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
return NO;
}
It looks like all of these answers take one approach, to simply deny the keyboard before it comes up. This prevents first responder status, which has many advantages.
One simple approach that allows you to maintain first responder status is to create an empty view and assign that to the inputView property on your input field. If you are using iOS 9 (or later?) you will also have to get rid of the inputAssistantItem objects as well.
UITextField *field = [[UITextField alloc] init];
field.inputView = self.emptyKeyboard.view;
UITextInputAssistantItem *aItem = [field inputAssistantItem];
aItem.leadingBarButtonGroups = #[];
aItem.trailingBarButtonGroups = #[];
Then if you want to control the field from an alternate view controller, you can do so by adding targets:
[field addTarget:self.numberPad action:#selector(editingBegan:) forControlEvents:UIControlEventEditingDidBegin];
[field addTarget:self.numberPad action:#selector(editingEnded:) forControlEvents:UIControlEventEditingDidEnd];
[field addTarget:self.numberPad action:#selector(fieldChanged:) forControlEvents:UIControlEventEditingChanged];
It is also possible to do this a lot more cleanly by subclassing UITextField.
Use the textField Delegate,
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
textField=nil;
return NO;
}
swift 3.0 version
First set the delegate for the text field
self.textfield.delegate = self
Then in an extension
extension ViewController: UITextFieldDelegate {
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
return false
}
}
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
// Here you can do for Specific text Field by
if (textField==(the text field you don't want to show keyboard)) {
NSLog(#"don't show keyboard");
return NO;
}
else {
return YES;
}
}
Swift 3/4
Add:- UITextFieldDelegate in your class.
Add:- self.textField.delegate = self In ViewDidLoad
last one just add this func -
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
return false
}

Tap on UITextField's clear button hides keyboard instead of clearing text

In iPhone, I have a view which has a UITextField. When I tap on the clear button of UITextField's the keyboard dismissed instead of clearing the text in the UITextField. On an iPad it is working correctly. What can I do to fix this?
Just clear the field, resignFirstResponder (if you want to hide keyboard) and return NO/false
Note: set Attributes inspector property of UITextField
Clear Button -> Appears while editing
so it will display the clear button while editing in the text field.
// Objective-C
-(BOOL)textFieldShouldClear:(UITextField *)textField
{
textField.text = #"";
[textField resignFirstResponder];
return NO;
}
// Swift
func textFieldShouldClear(textField: UITextField) -> Bool {
textField.text = ""
textField.resignFirstResponder()
return false
}
Try this code after you attach delegate of uitextifield
-(BOOL)textFieldShouldClear:(UITextField *)textField
{
return true;
}
First, check all the code blocks that related to your UITextField (especially the code yourTextField.hidden = YES;)
Put break points and analyze every UITextField delegates that you implemented.
(textFieldDidEndEditing,textFieldShouldEndEditing,textFieldShouldReturn.etc.)
OR
Implement the textFieldShouldClear delegate and write the code here to visible and clear your UITextField
To do this, you have to set the clearButtonMode as below,
yourTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
yourTextField.delegate = self;
//For active keyboard again
[yourTextField becomeFirstResponder];
Then implement the textFieldShouldClear delegate
YourClass.h
#interface className : UIViewController <UITextFieldDelegate>
YourClass.m
-(BOOL)textFieldShouldClear:(UITextField *)textField {
yourTextField.hidden = NO;
yourTextField.text = #"";
return YES;
}
Just make sure U've given these two
editingTextField.delegate = self;
editingTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
TextFieldShouldClear is needed only if you need to do some customizations :-)
Are you doing some thing in this method?
Maybe you are are calling resignFirstResponder in this delegate method, thats why the keyboard is getting dismissed.
Please go through the delegate methods, and check what u r doing exactly.
This issue happened also if you have
yourTextField.clearButtonMode = UITextFieldViewModeNever;
Check this line and delete it or change view mode..

Resources