Hide keyboard when editing another textfield - ios

I have 4 textfields, when editing 3 of them I want the standard keyboard which works fine, but on the 4th textfield i want to display a picker wheel instead.
My code works fine when pressing on the 4th textfield directly, but if I edit textfield 1-3 and then press on number 4, the keyboard doesn't remove.
Any ideas why? and how to solve it?
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
if ([textField.text isEqualToString:selectedCountryText])
{
NSLog(#"Edit country");
[self.view endEditing:YES];
[UIView beginAnimations: #"anim" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: 0.3f];
if (pickerVisible)
{
picker.frame = CGRectOffset(picker.frame, 0, -341);
pickerVisible = FALSE;
}
else
{
picker.frame = CGRectOffset(picker.frame, 0, 341);
pickerVisible = TRUE;
}
[UIView commitAnimations];
[self removeKeyboard];
}
else
{
if (pickerVisible)
{
//Remove picker
[UIView beginAnimations: #"anim" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: 0.3f];
picker.frame = CGRectOffset(picker.frame, 0, -341);
[UIView commitAnimations];
pickerVisible = FALSE;
}
}
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
if ([textField.text isEqualToString:selectedCountryText])
{
//do nothing for now
}
else
{
[self.view endEditing:YES];
[self removeKeyboard];
}
}
- (void) removeKeyboard
{
[self.firstname resignFirstResponder];
[self.lastname resignFirstResponder];
[self.year resignFirstResponder];
[self.country resignFirstResponder];
}
In The view did load I have set the delegate.
- (void)viewDidLoad
{
NSLog(#"viewdidload SearchSwimmerView");
// Setup the text fields..
[self.firstname setDelegate:self];
[self.firstname setReturnKeyType:UIReturnKeyDone];
[self.firstname addTarget:self action:#selector(removeKeyboard) forControlEvents:UIControlEventEditingDidEndOnExit];
[self.lastname setDelegate:self];
[self.lastname setReturnKeyType:UIReturnKeyDone];
[self.lastname addTarget:self action:#selector(removeKeyboard) forControlEvents:UIControlEventEditingDidEndOnExit];
[self.year setDelegate:self];
[self.year setReturnKeyType:UIReturnKeyDone];
[self.year addTarget:self action:#selector(removeKeyboard) forControlEvents:UIControlEventEditingDidEndOnExit];
[self.country setDelegate:self];
}

Instead of using [self.view endEditing:YES] Use
[textField performSelector:#selector(resignFirstResponder)
withObject:nil
afterDelay:0];

I think setting inputView for 4th textfield as PickerView may do the trick.
textfield.inputView = pickerView;

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
if(textField==fourthtextfield)
{
return NO;
}
}

Related

ios keyboard is not hiding on resignfirstresponder

Added a subview on App delegate window, textfield is in subview, startDateTxtFld will open date picker but date picker remains behind the keyboard. keyboard does not hide on resignFirstResponder or self.view eneEditing setting as true.
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
if (textField == startDateTxtFld )
{
startDateSelected =YES;
endDateSelected = NO;
// [referenceNmbrTxtFld resignFirstResponder];
// [startDateTxtFld becomeFirstResponder];
//textField.inputView=datePicker;
// [self.view endEditing:YES];
// [[UIApplication sharedApplication] sendAction:#selector(resignFirstResponder) to:nil from:nil forEvent:nil];
// [APP_DELEGATE.window resignFirstResponder];
[[[UIApplication sharedApplication] keyWindow] endEditing:YES];
[UIView animateWithDuration:0.5 animations:^
{
[startDateTxtFld resignFirstResponder];
_bottomGap4DatePicker.constant=0;
datePicker.backgroundColor=[UIColor whiteColor];
// [_subVw4DtPkr becomeFirstResponder];
}];
[self.navigationController.view endEditing:YES];
}
else if (textField == endDateTxtFld)
{
startDateSelected =NO;
endDateSelected = YES;
// [self.view endEditing:YES];
// [referenceNmbrTxtFld resignFirstResponder];
// [endDateTxtFld becomeFirstResponder];
// textField.inputView=datePicker;
// [[UIApplication sharedApplication] sendAction:#selector(resignFirstResponder) to:nil from:nil forEvent:nil];
// [APP_DELEGATE.window resignFirstResponder];
[[[UIApplication sharedApplication] keyWindow] endEditing:YES];
[UIView animateWithDuration:0.5 animations:^
{
// [endDateTxtFld resignFirstResponder];
_bottomGap4DatePicker.constant=0;
datePicker.backgroundColor=[UIColor whiteColor];
//[_subVw4DtPkr becomeFirstResponder];
}];
[self.navigationController.view endEditing:YES];
}
else
{
[textField becomeFirstResponder];
_bottomGap4DatePicker.constant=[UIScreen mainScreen].bounds.size.height +_subVw4DtPkr.frame.size.height;
_popUpVwConstraint.constant=-30;
}
}
Hide keyboard Anywhere in IOS :
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UIView * txt in self.view.subviews){
if ([txt isKindOfClass:[UITextField class]] && [txt isFirstResponder]) {
[txt resignFirstResponder];
}
}
}
OR
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.view endEditing:YES];
}
I tried all the solution, like adding
UITextFieldDelegate
tf_edit.returnKeyType = UIReturnKeyDone
- (BOOL)disablesAutomaticKeyboardDismissal
{
return NO;
}
Nothing helped.
textFieldShouldReturn was being called. but keyboard was not appearing. I read that it is going to some other textfield when I am clicking DONE button. But there was no other textfield. So instead of applying resignFirstResponder to my textfield name, I applied to the textfield which is a parameter of textFieldShouldReturn function and it worked. I am not sure if it is correct way. It worked for me.
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
//[tf_edit resignFirstResponder];
[textField resignFirstResponder];
return YES;
}
(BOOL)textFieldShouldBeginEditing:(UITextField *)textField { {
NSLog(#"textFieldDidBeginEditing===%#" , textField.text);
CGAffineTransform translation = CGAffineTransformIdentity;
if (textField==vacDate || textField==vacTime) {
[self.view endEditing:YES];
[UIView beginAnimations:nil context:nil];
self.view.transform = translation;
[UIView commitAnimations];
myDatePicker.hidden = NO;
return NO;
}
else
{
if (textField==vacType) {
translation = CGAffineTransformMakeTranslation(0, -60);
}
[UIView beginAnimations:nil context:nil];
self.view.transform = translation;
[UIView commitAnimations];
myDatePicker.hidden = YES;
return YES;
}
}

Set UITextField delegate to all UITextFields and set view animation for few textfields

I have 13 text fields. I want to set view animation for only 4 text fields which will be displayed in bottom
Here is my code:
if (textField.editing != ((_GET_companyname)||(_GET_firstname)||(_GET_middlename)||(_GET_lastname)||(_GET_companyurl)||(_GET_streetaddress)||(_GET_postbox)||(_GET_code)||(_GET_phonenum)))
{
NSLog(#"Entered in condition");
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
//Keyboard becomes visible
[UIView beginAnimations:nil context:NULL];
// [UIView setAnimationDuration:0.25];
self.view.frame = CGRectMake(0,-200,self.view.frame.size.width,self.view.frame.size.height);
[UIView commitAnimations];
}
}
else
{
NSLog(#"Entered else Part");
}
I need to set the below to all 14 textfields
-(BOOL) textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
According to you if "_GET_companyname" is a IBOutlet of UITextField.
Then you should write something like this below
if ((textField != _GET_companyname)&&(textField != _GET_firstname)) // etc
{
NSLog(#"Entered in condition");
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
//Keyboard becomes visible
[UIView beginAnimations:nil context:NULL];
// [UIView setAnimationDuration:0.25];
self.view.frame = CGRectMake(0,-200,self.view.frame.size.width,self.view.frame.size.height);
[UIView commitAnimations];
}
}
else
{
NSLog(#"Entered else Part");
}
Edit :- And also instead of checking (!=). you should check (==) for few UITextFields. That will be better.
You can check these using tags
- (void)viewDidLoad {
[super viewDidLoad];
UITextField *textFiledName = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
textFiledName.tag = 0;
textFiledName.delegate = self;
[self.view addSubview:textFiledName];
UITextField *textFiledLastName = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
textFiledLastName.tag = 1;
textFiledLastName.delegate = self;
[self.view addSubview:textFiledLastName];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
if (textField.tag == 0) {
//No Animation
}else if (textField.tag == 1){
//Add Animation
}
return YES;
}

UIView in iOS 8 Not Moving up when a UITextFiled Editing Begins

I have written the below Logic for moving a view upwards when a textfiled begins its editing.It is working fine in iOS 7 but it is not working in iOS 8.
Code:
-(void)viewDidLoad
{
_leagalName.delegate = self;
_phoneNumber.delegate = self;
_email.delegate = self;
_contactName.delegate = self;
_contactNumber.delegate = self;
_password.delegate = self;
_confirmPassword.delegate = self;
}
-(void)animateTextField:(UITextField*)textField up:(BOOL)up
{
const float movementDuration = 0.3f; // tweak as needed
int movement = (up ? movementDistance : -movementDistance);
[UIView beginAnimations: #"animateTextField" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
self.sampleView.frame = CGRectOffset(self.sampleView.frame, 0, movement);
[UIView commitAnimations];
}
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
if([textField isEqual:_leagalName])
{
movementDistance = 0;
[self animateTextField:textField up:YES];
}
else if([textField isEqual:_phoneNumber])
{
movementDistance = 0;
[self animateTextField:textField up:YES];
}
else if([textField isEqual:_email])
{
movementDistance = -40;
[self animateTextField:textField up:YES];
}
else if([textField isEqual:_contactName])
{
movementDistance = -80;
[self animateTextField:textField up:YES];
}
else if([textField isEqual:_contactNumber])
{
movementDistance = -110;
[self animateTextField:textField up:YES];
}
else if([textField isEqual:_password])
{
movementDistance = -140;
[self animateTextField:textField up:YES];
}
else if([textField isEqual:_confirmPassword])
{
movementDistance = -170;
[self animateTextField:textField up:YES];
}
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[self animateTextField:textField up:NO];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
Can anyone please explain how i can move view upwards in iOS 8.
in iOS8, it is not recommended to change frame during animation for auto-layout case.
The common method used by most developers would be updating the constrains accordingly in your animation block.
If you still want to try the set frame method, probably you can disable auto-layout and give a try.
Please use following methods for updating you UI
- (void)keyboardDidShow:(NSNotification *)note
{
[UIView animateWithDuration:0.5f animations:^ {
if([super isIpad]){
self.view.frame = CGRectMake(0, -350, 768, 1024);
}
else{
self.view.frame = CGRectMake(0, -275, 320, 568);
}
}];
}
-(void)keyboardDidhide:(NSNotification *)note
{
[UIView animateWithDuration:0.5f animations:^ {
if([super isIpad]){
self.view.frame = CGRectMake(0, 0, 768, 1024);
}
else{
self.view.frame = CGRectMake(0, 0, 320, 568);
}
}];
}
remember that i am using [UIWindow sharedWindow] to draw my nib, you mush be using your screen size and secondly please add following code in your viewDidLoad method.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
`[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardDidhide:) name:UIKeyboardDidHideNotification object:nil];
`

View is not set back to original position

In my xib i have set one text box ...when user edit in it i am moving that view little up...but when i return back it to its original postion it not come to its original postion it remain little up then original postion.
Here is viewDidLoad
- (void)viewDidLoad
{
[super viewDidLoad];
if ([self respondsToSelector:#selector(edgesForExtendedLayout)])self.edgesForExtendedLayout = UIRectEdgeNone;
search=FALSE;
UIButton *btnOther = [UIButton buttonWithType:UIButtonTypeSystem]; //UIButtonTypeCustom for image button
[btnOther setTitle:#"Save" forState:UIControlStateNormal];
btnOther.titleLabel.font = [UIFont fontWithName:GZFont size:12.0f];
// [btnSave setImage:[UIImage imageNamed:#"save.png"] forState:UIControlStateNormal];
[btnOther addTarget:self action:#selector(btnSaveAction:) forControlEvents:UIControlEventTouchUpInside];
[btnOther setFrame:CGRectMake(0, 0, 55, 29)];
dataArray=[[NSMutableArray alloc]init];
nameSearchArray=[[NSMutableArray alloc]init];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btnOther];
txtCharityName.hidden=YES;
txtCharityMail.hidden=YES;
originalCenter=self.view.center;
}
here is textfield delegate methods.:
- (void)textFieldDidBeginEditing:(UITextField *)textField {
if(textField==txtGratuity){
self.view.center=CGPointMake(originalCenter.x, originalCenter.y-30);
}
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
self.view.center=CGPointMake(originalCenter.x, originalCenter.y);
charityId=#"";
NSLog(#"charityID%#",charityId);
}
and here is screenshot.:
as per this equation
frame.origin = center - (bounds.size / 2.0)
center = frame.origin + (bounds.size / 2.0)
you should add 30 points for textfield to get to correct position.
- (void)textFieldDidEndEditing:(UITextField *)textField {
self.view.center=CGPointMake(originalCenter.x, originalCenter.y+30);
charityId=#"";
NSLog(#"charityID%#",charityId);
}
Edit:
try this in your
- (void)textFieldDidBeginEditing:(UITextField *)textField {
[UIView animateWithDuration:0.25
delay:0.0
options: UIViewAnimationOptionCurveEaseInOut
animations:^{
CGRect frame;
// let's move our textField
frame = textField.frame;
frame.origin.y = frame.origin.y-30;
textField.frame=frame;
}
completion:^(BOOL finished){
if(finished) NSLog(#"Finished !!!!!);
}];
}
and move textfield down after edit
- (void)textFieldDidEndEditing:(UITextField *)textField {
[UIView animateWithDuration:0.25
delay:0.0
options: UIViewAnimationOptionCurveEaseInOut
animations:^{
CGRect frame;
// let's move our textField
frame = textField.frame;
frame.origin.y = frame.origin.y+30;
textField.frame=frame;
}
completion:^(BOOL finished){
if(finished) NSLog(#"Finished !!!!!);
}];
}
Set the textfield frames in viewWillAppear instead of viewDidLoad method.

setFrame won't do anything

I have a problem with an app that won't set frames outside -init and -viewWillLayoutSubviews methods. What should happen when one taps the editButton is an animation that will hide the editor view. Nonetheless, nothing happens as I test it. The problem doesn't come from the animation method since the -setFrame method as it - not included in the block - doesn't work neither.
Here is the code :
-(id)init {
if (self = [super init]) {
editButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:#selector(editButtonTapped)];
doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneButtonTapped)];
editor = [[UIView alloc] init];
[editor setBackgroundColor:[UIColor yellowColor]];
editor.clipsToBounds = YES;
editorIsOpen = YES;
portraitRegularModeEditorRect = CGRectMake(15, 59, 738, 100);
portraitClosedEditorEditorRect = CGRectMake(15, 59, 738, 0);
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
[[self view] addSubview:editor];
[[self view] setBackgroundColor:[UIColor blueColor]];
}
-(void)viewDidAppear:(BOOL)animated {
[self setForRegularMode];
}
-(void)viewWillLayoutSubviews {
UIInterfaceOrientation io = [[UIApplication sharedApplication] statusBarOrientation];
if (io == UIInterfaceOrientationPortrait || io == UIInterfaceOrientationPortraitUpsideDown) {
//portrait
[editor setFrame:portraitRegularModeEditorRect];
} else {
//landscape
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(void)editButtonTapped {
[self setForScenarioLinesEditingMode];
}
-(void)doneButtonTapped {
[self setForRegularMode];
}
-(void)setForRegularMode {
editingMode = CPRegularMode;
if (!editorIsOpen) {
[UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationCurveEaseOut animations:^(void){
[editor setFrame:portraitRegularModeEditorRect];
} completion:^(BOOL finished) {
editorIsOpen = YES;
}];
}
[[self navigationItem] setRightBarButtonItems:[[NSArray alloc] initWithObjects:editButton,nil]];
}
-(void)setForScenarioLinesEditingMode {
editingMode = CPScenarioLinesEditingMode;
if (editorIsOpen) {
[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationCurveEaseOut animations:^(void){
[editor setFrame:portraitClosedEditorEditorRect];
} completion:^(BOOL finished) {
editorIsOpen = NO;
}];
}
[[self navigationItem] setRightBarButtonItems:[[NSArray alloc] initWithObjects:doneButton,nil]];
}
If anyone can help, thanks in advance ;)
I think that the problem in your case is the fact that in -(void)viewWillLayoutSubviews method you set, lets say the default frame of your view, if you try to change the frame in other methods after the setFrame is called on your view, the -(void)viewWillLayoutSubviews will also be called and the frame of the view will be the default one. Try to remove the setFrame from your -(void)viewWillLayoutSubviews.
Is your view controller set up in storyboards, and are you using Autolayout (which is on by default?) If so, setFrame won't work and you need to edit constraints after creating outlets to them from the storyboard.
Alternatively, you can turn off Autolayout in your storyboard, as shown here.

Resources