How to show Xbutton(clear button) always visible in uisearchbar [duplicate] - ios

This question already has answers here:
Adding the "Clear" Button to an iPhone UITextField
(11 answers)
Closed 2 years ago.
In my application, I am adding a UISearchBar.
My intent is to enable the UISearch Bar "X button"(clear button in UITextField) to be always visible.
I have tried using the following code below to try to make the "X Button" be always visible. However, it does not work. If I set tf.clearButtonMode = UITextFieldViewModeNever, the clear button in uitextfield not showing. I am not sure what is wrong?
I would really appreciate anyone's help here. Why is this not working?
Code (Not working)
for (UIView* v in searchBar.subviews)
{
if ( [v isKindOfClass: [UITextField class]] )
{
UITextField *tf = (UITextField *)v;
tf.delegate = self;
tf.clearButtonMode = UITextFieldViewModeAlways;
break;
}
}
Goal:
I want to always show the clear button if the text length is equal to 0
i.e. if I don't input any text.

UITextField *searchBarTextField = nil;
for (UIView *subview in self.searchBar.subviews)
{
if ([subview isKindOfClass:[UITextField class]])
{
searchBarTextField = (UITextField *)subview;
searchBarTextField.clearButtonMode = UITextFieldViewModeAlways;
break;
}
}

This is the default behavior of the search bar. Because if the UITextField is blank then there is no need to press it.

U can do it in Xib. I am attaching the screenshot.
And programmatically
myUITextField.clearButtonMode = UITextFieldViewModeAlways;

I tried to get it but unfortunately , There is no Way of Customising with the ClearButton(X) of UITextField .
There is a way that If You only need it to get resign the KeyBoard , Then just overriding this method :
Just clear the field yourself and call resignFirstResponder .
-(BOOL)textFieldShouldClear:(UITextField *)textField
{
textField.text = #"";
[textField resignFirstResponder];
return NO;
}
Documentation about it HERE

This is an older question but I came here with an equal customer request: "Show the clearButton as soon as the cursor is in the searchField. We want to be able to cancel the search with this button in any stage".
I came up with a solution other than adding a custom button:
AppleDocs:
UITextFieldViewModeAlways The overlay view is always displayed if the
text field contains text.
So adding a whitespace as the first character will set the clearButton active.
The leading whitespace can be removed as soon as text is entered in the searchField or at any other point before using the text.
-(void)textFieldDidBeginEditing:(UITextField *)textField{
//adding a whitespace at first start sets the clearButton active
textField.text = #" ";
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
...
NSString *completeNewString = [textField.text stringByReplacingCharactersInRange:range withString:string];
//remove the dummyWhitespace (here or later in code, as needed)
self.searchString = [completeNewString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
...
return YES;
}

Related

iOS: Make a UITextField like WhatsApp Verification Code TextField?

My problem is that I want to make a UITextField like WhatsApp verification code textField(like below image).Where default value contains number of character to be entered(as hint for user). When user enter text replace hint character with entered character one by one?
Any help is much appreciated.
Please refer below link of my previous answer. Hope you will get an idea how to do this. Only single UITextField, No third party library use.
link- https://stackoverflow.com/a/36769911/5097148
for don't use of paste make couple of changes-
add myLable above textField then add this code in viewDidLoad()
self.myLable.userInteractionEnabled=true;
UITapGestureRecognizer *LongPressgesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapPressgesture:)];
tapPressgesture.delegate=self;
[self.myLable addGestureRecognizer:tapPressgesture];
and
- (void)tapPressgesture:(UITapPressGestureRecognizer *)recognizer
{
[self.txtField becomeFirstResponder];
}
for background like your image you can set textField background image property or add bottom border to your textfield. UITextField border style must be UITextBorderStyleNone If you have any issue then let me know.
Happy coding:)
After adding 6 Textfields and background image.
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
return YES;
}
-(void)textFieldDidEndEditing:(UITextField *)textField{
if (textField==textfield1)
{
[textfield1 resignFirstResponder];
[textField2 becomeFirstResponder];
}
else if (textField==textfield2)
{
[textfield2 resignFirstResponder];
[textField3 becomeFirstResponder];
}
}
continue till textfield 6

Detect when a user presses the space button

I currently have my keyboard set so the type is Numbers and Punctuation [so users can type in a hypen symbol]. Does anyone know how to detect if the user presses the space bar so I can automatically set the style to a different keyboard type [in my case letters] so I don't have to build a custom keyboard?
Try this code - I am sure that it will detect if a space character is coded in - I have not really tested how well the keyboard switch works. You should also try it out without the resignfirstresponder becomefirstresponder code once the code below is working. Good Luck! (PS your viewCOntroller needs to be declared as a UITextview or textfield Delegate)
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText: (NSString *)text_message {
if ([text_message isEqual:#" "]) {
// Put your code to change the keyboard then refresh the screen.
[textView resignFirstResponder]; //close textview or textfield
[textView setKeyboardType:UIKeyboardTypeDefault];
[textView becomeFirstResponder]; // opens keyboard for textfield or textview
[self.view setNeedsRefresh]; // refresh view
// return NO;
}
return YES; //go back to editing
}

Disable Keyboard for UITextfield

I'm wondering how to disable the inputview of a UITextfield. Setting textField.inputView = nil; or [textField setInputView:nil] in ShouldBeginEditing doesn't do anything, and using the userInteraction property removes the ability to interact with the field. Ideally, I'd like to remove both the cursor and the keyboard while still being able interact with and switch between textfield methods, using ShouldBeginEditing and ShouldEndEditing. Is there any way to accomplish this?
You should do this:
myTextField.inputView = UIView.new; //Empty UIView
Setting it to nil just means the default keyboard is used.
To get rid of the caret, subclass the UITextField and override caretRectForPosition:
- (CGRect) caretRectForPosition:(UITextPosition*)position
{
return CGRectZero;
}
Try this :
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
return NO; // Hide both keyboard and blinking cursor.
}
or
-(void)textFieldDidBeginEditing:(UITextField *)textField{
[textField resignFirstResponder]; // hides keyboard
}

Changing UITextField borderstyle in didEndEditing

I want to highlight a uitextfield when a user is editing it, so I set my textfield's borderstyle default to UITextBorderStyleNone and use the uitextfields delegates as following:
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[textField setBorderStyle:UITextBorderStyleBezel];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[textField setBorderStyle:UITextBorderStyleNone];
}
The Bezel style gets set and rendered, but when the endediting is called, the none style is not applied.
I tried changing the none to another (say rounded rect), but that one does render properly.
Does anybody know how I can get this to work?
Yes, it is a bug. Anyway, I've solved by using this code:
textField.borderStyle = UITextBorderStyleLine;
textField.borderStyle = UITextBorderStyleNone;
This isn't a very beautiful method, but it does the work until Apple fixes this issue.
I had this issue also. From my testing it looked as if I could set it to any style other than UITextBorderStyleNone.
A workaround that worked for me at least was disabling and re-enabling the textfield as seen below:
- (void)textFieldDidEndEditing:(UITextField *)textField {
textField.enabled = NO;
textField.borderStyle = UITextBorderStyleNone;
textField.enabled = YES;
}
I don't really like it but it works for now.

UITextView setSelectedRange changes editable property

From the start, my UITextView with editable property to true is not editable (via settings in a NIB). The behaviour is such that a user can select and copy text but not edit. This is the way things should be.
However, if I make a call to setSelectedRange, a side effect is that the editable property is set to YES.
Setting it back to NO [textview setEditable:NO] scrolls to the bottom of the textView and undoes my programmatic selection. It also doesn't work, as editing becomes enabled anyway. The keyboard appears and everything.
I need to be able to select something programmatically and keep the textView in a state where users can only copy and select text.
[textView select:self];
[textView setSelectedRange:selectedText];
I'm stuck. Looking for any advice you can give.
This doesn't work:
[textView select:self];
[textView setSelectedRange:selectedText];
[textView setEditable:NO];
I've also tried setting the delegate function textViewShouldBeginEditing to return NO:
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
return NO;
}
That just locks everything down and I can't select any text.
I realize this has already been answered, but here is an improvement I made to keep the popout menu, just disable certain options;
-(BOOL) canPerformAction:(SEL)action withSender:(id)sender {
bool response = [super canPerformAction:action withSender:sender];
if(response && (action == #selector(cut:) || action == #selector(paste:) || action == #selector(delete:) || action == #selector(_promptForReplace:))) {
return NO;
}
return response;
}
canPerformAction is called per defined action. Calling the parent method will take care of most of these cases, but I have also decided to disable cut,paste,delete and spelling suggestions (_promptForReplace).
This appears to work for me in a similar situation:
just let the textview be editable
[textView setDelegate:self];
[textView select:self];
[textView setSelectedRange:range];
add a function (BOOL)textView:shouldChangeTextInRange:replacementText: returning NO
and the final trick: assign an empty view as a keyboard for the textview, using:
textView.inputView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
The textView should be editable:
[textView setEditable:YES];
Do the selection:
[textView select:self];
[textView setSelectedRange:range];
Have these in the delegate:
To disable the menu (not ideal for me but it's ok):
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
[UIMenuController sharedMenuController].menuVisible = NO;
return NO;
}
To disable the keyboard:
textView.inputView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
To disable editing:
- (BOOL)textView:(UITextView*)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text {
return NO;
}

Resources