I have a Scrolleview and i had its container View a UIView and UIVew contains textfields but Keyboard is not Poping..
I had use this methods...
-(void)scrollViewAdaptForEditingtext:(UITextField*)textfield {
CGPoint point = CGPointMake(0, textfield.frame.origin.y - 1.5 * textfield.frame.size.height);
[self.scrollView setContentOffset:point animated:YES];
}
-(void)scrollViewEndEditingtext:(UITextField*)textfield {
CGPoint point = CGPointMake(0, 0);
[self.scrollView setContentOffset:point animated:YES];
}
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
[self scrollViewAdaptForEditingtext:textField];
return YES;
}
#pragma TextFieldDelegateProtocol
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
if (textField == self.txtFieldFirstName) {
[self.txtFieldLastName becomeFirstResponder];
} else if (textField == self.txtFieldLastName) {
[self.txtFieldEmail becomeFirstResponder];
} else if (textField == self.txtFieldEmail) {
[self.txtFieldMobNumber becomeFirstResponder];
} else if (textField == self.txtFieldMobNumber) {
[self.txtFieldGender becomeFirstResponder];
} else
[textField resignFirstResponder];
[self scrollViewEndEditingtext:textField];
return YES;
}
can I include uiview also
Related
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;
}
}
How can I do it with out resize view frame (or tableView)?
I use this code and I have resized view then I scroll down/up
I use code, not storyboard
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
if ([gestureRecognizer class] == [UIPanGestureRecognizer class])
{
UIPanGestureRecognizer *panGestureRec = (UIPanGestureRecognizer *)gestureRecognizer;
CGPoint distance = [panGestureRec translationInView:self.tableView];
if (distance.y > 0 || distance.y < 0)
{
if (distance.y > 0) // down
{
//NSLog(#"user swiped down");
[self.navigationController setNavigationBarHidden:YES animated:YES];
} else if (distance.y < 0) //up
{
//NSLog(#"user swiped up");
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
return NO;
} else {
return YES;
}
}
return YES;
}
Try this code . This will give smooth animation (Used UIView Animation property)
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
if ([gestureRecognizer class] == [UIPanGestureRecognizer class])
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:2];
UIPanGestureRecognizer *panGestureRec = (UIPanGestureRecognizer *)gestureRecognizer;
CGPoint distance = [panGestureRec translationInView:self.tableView];
if (distance.y > 0 || distance.y < 0)
{
if (distance.y > 0) // down
{
//NSLog(#"user swiped down");
[self.navigationController setNavigationBarHidden:YES animated:YES];
} else if (distance.y < 0) //up
{
//NSLog(#"user swiped up");
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
return NO;
} else {
return YES;
}
[UIView commitAnimations];
}
return YES;
I have iphone app in which i am calling code one on textFieldShouldReturn and other on didEndEditing.
I want that when user return press the app should not called didEndEditing code and if user does not press return then call.
-(void)textFieldDidEndEditing:(UITextField *)textField
{
if (textField==tagTextField) {
[self showAnimationBack];
}
if (textField.tag==2 && [valueReturn isEqualToString:#"Yes"]) {
if (textField.text.length > 0 || ![tagTextField.text isEqualToString:#""]) {
[textField resignFirstResponder];
[tagArray addObject:tagInputField.text];
[tableView reloadData];
tableView.frame = CGRectMake(tableView.frame.origin.x, tableView.frame.origin.y, tableView.frame.size.width, tableView.contentSize.height);
[tableView.layer setCornerRadius:7.0f];
[tableView.layer setMasksToBounds:YES];
tableView.layer.borderWidth = 0.5;
tableView.layer.borderColor = [UIColor grayColor].CGColor;
[self showAnimationBack];
}
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if(textField.tag == 2)
{
if (textField.text.length > 0 || ![tagTextField.text isEqualToString:#""]) {
[textField resignFirstResponder];
[tagArray addObject:tagInputField.text];
[tableView reloadData];
tableView.frame = CGRectMake(tableView.frame.origin.x, tableView.frame.origin.y, tableView.frame.size.width, tableView.contentSize.height);
[tableView.layer setCornerRadius:7.0f];
[tableView.layer setMasksToBounds:YES];
tableView.layer.borderWidth = 0.5;
tableView.layer.borderColor = [UIColor grayColor].CGColor;
[self showAnimationBack];
}
}
I call this also when user press return
- (void)keyboardDidHide:(NSNotification *)aNotification {
valueReturn=#"Yes";
}
Use isEditing value, as you (probably) resign the text view from first responder when the return key is pressed, textFieldDidEndEditing should have the text field at not-editing mode when it happens.
I am not sure if this a proper way of doing it but you can try this. Declare a variable isReturnPressed and make it YES in textFieldShouldReturn: and make it NO in textFieldDidEndEditing: . So you can check the condition as follows..
-(void)textFieldDidEndEditing:(UITextField *)textField
{
if (isReturnedPressed == NO) {
// Do your stuff
}
else {
isReturnedPressed = NO
}
}
Hope this helps.
i have four textfields. User is allowed to enter only one digit in each text field.Once the user Enters single digit its focus should be on the next textfield. i have done this part and its working fine. Now, What i want is when i remove the text from the text field then its focus should move to previous textfield. i mean if i am deleting the text(digit) from the fourth text field then its focus should move to third text field. In short on removal of text the focus should be on previous textfield.
Now, what my problem is when i remove the text from the textfield then its focus moves to previous textfield but it clears the text of that textfield(the textfield on which i have set the focus).What i want is the text should not be removed on focus.
in .h file
IBOutlet UITextField *txtPinDigit1;
IBOutlet UITextField *txtPinDigit2;
IBOutlet UITextField *txtPinDigit3;
IBOutlet UITextField *txtPinDigit4;
UITextField *currentTextField;
in .m file
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if(textField.tag==0)
{
currentTextField=txtPinDigit1;
}
else if(textField.tag==1)
{
currentTextField=txtPinDigit2;
}
else if(textField.tag==2)
{
currentTextField=txtPinDigit3;
if(isDelete)
{
textField.text=digit3;
}
}
else if(textField.tag==3)
{
currentTextField=txtPinDigit4;
}
return YES;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if(([textField.text length]==1)&&(![string isEqualToString:#""]))
{
if(currentTextField==txtPinDigit1)
{
[txtPinDigit2 becomeFirstResponder];
}
else if(currentTextField==txtPinDigit2)
{
[txtPinDigit3 becomeFirstResponder];
}
else if(currentTextField==txtPinDigit3)
{
[txtPinDigit4 becomeFirstResponder];
}
else if(currentTextField==txtPinDigit4)
{
textField.text = [textField.text substringToIndex:MAXLENGTH-1];
//[txtPinDigit4 resignFirstResponder];
//[txtPinDigit1 becomeFirstResponder];
}
}
else if([string isEqualToString:#""])
{
isDelete=YES;
NSLog(#"replacementString:%#",string);
// textField.text=string;
if(currentTextField==txtPinDigit4)
{
textField.text=string;
digit3=nil;
[digit3 release];
digit3=[[NSString alloc] initWithFormat:#"%i",[txtPinDigit3.text intValue]];
[txtPinDigit3 becomeFirstResponder];
}
else if(currentTextField==txtPinDigit3)
{
textField.text=string;
[txtPinDigit2 becomeFirstResponder];
}
else if(currentTextField==txtPinDigit2)
{
textField.text=string;
[txtPinDigit1 becomeFirstResponder];
}
else if(currentTextField==txtPinDigit1)
{
textField.text=string;
// [txtPinDigit1 resignFirstResponder];
//[txtPinDigit1 becomeFirstResponder];
}
}
return YES;
}
in the above code MAXLENGTH=1 defined.
any help will be appreciated.
thanks in advance.
Try This code, Change shouldChangeCharactersInRange: Delegate Method
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
if ([newString length] < 1)
{
if (textField.tag==2)
{
[txtPinDigit1 performSelector:#selector(becomeFirstResponder) withObject:nil afterDelay:0.0];
// txt2.text=#"";
}
else if (textField.tag==3)
{
[txtPinDigit2 performSelector:#selector(becomeFirstResponder) withObject:nil afterDelay:0.0];
}
else if (textField.tag==4)
{
[txtPinDigit3 performSelector:#selector(becomeFirstResponder) withObject:nil afterDelay:0.0];
}
return YES;
} else
{
// Otherwise we cut the length of newString to 1 (if needed) and set it to the textField.
textField.text = [newString length] > 1 ? [newString substringToIndex:1] : newString;
if (textField.tag==1)
{
[textField resignFirstResponder];
[txtPinDigit2 becomeFirstResponder];
}
else if (textField.tag==2)
{
[textField resignFirstResponder];
[txtPinDigit3 becomeFirstResponder];
}
else if (textField.tag==3)
{
[textField resignFirstResponder];
[txtPinDigit4 becomeFirstResponder];
}
return NO;
}
}
Check to check length via this way and put a break point at beginning of shouldChangeCharactersInRange
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
int length = textField.text.length - range.length + string.length;
return YES;
}
I hope it will help you.
Before calling up to become first responder on filling up the 1st digit,bind the entered text inputs & the responder into dictionary...
On each previous/next condition,retrieve the information from the dictionary...The below example code is for the general previous/next custom actions(not auto though)..You just need to change the logic a bit based on the condition you prefer...
- (NSArray *) responders
{
if (_responders)
return _responders;
NSArray *textInputs = EditableTextInputsInView([[UIApplication sharedApplication] keyWindow]);
return [textInputs sortedArrayUsingComparator:^NSComparisonResult(UIView *textInput1, UIView *textInput2) {
UIView *commonAncestorView = textInput1.superview;
while (commonAncestorView && ![textInput2 isDescendantOfView:commonAncestorView])
commonAncestorView = commonAncestorView.superview;
CGRect frame1 = [textInput1 convertRect:textInput1.bounds toView:commonAncestorView];
CGRect frame2 = [textInput2 convertRect:textInput2.bounds toView:commonAncestorView];
return [#(CGRectGetMinY(frame1)) compare:#(CGRectGetMinY(frame2))];
}];
}
The below function should get called before doing previous/next auto tab.
- (Void) selectAdjacentResponder: (UISegmentedControl *) sender
{
NSArray *firstResponders = [self.responders filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(UIResponder *responder, NSDictionary *bindings) {
return [responder isFirstResponder];
}]];
NSLog(#"%#",firstResponders);
UIResponder *firstResponder = [firstResponders lastObject];
NSInteger offset = sender.selectedSegmentIndex == 0 ? -1 : +1;
NSInteger firstResponderIndex = [self.responders indexOfObject:firstResponder];
NSInteger adjacentResponderIndex = firstResponderIndex != NSNotFound ? firstResponderIndex + offset : NSNotFound;
UIResponder *adjacentResponder = nil;
if (adjacentResponderIndex >= 0 && adjacentResponderIndex < (NSInteger)[self.responders count])
adjacentResponder = [self.responders objectAtIndex:adjacentResponderIndex];
[adjacentResponder becomeFirstResponder];
}
Bit lengthier,but this is all i know a BIT :)..Happy coding...
In Apple iOs photos app, Each picture take the full screen, but when You tap on it, navigation bar and tab bar with some menu options (like share picture) just appear and remain for a couple of secconds. How can I do that in my UIImageView ?
Add a UITapGestureRecognizer to your view and UiView for your topbar and bottom bar or what else you like and follow below code. I think This may help you.
//Write below code in ViewDidLoad
UITapGestureRecognizer *singleTapOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
singleTapOne.numberOfTouchesRequired = 1; singleTapOne.numberOfTapsRequired = 1; singleTapOne.delegate = self;
[self.view addGestureRecognizer:singleTapOne]; [singleTapOne release];
for (UIGestureRecognizer *gR in self.view.gestureRecognizers) {
gR.delegate = self;
// handleSingleTap Method
- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer
{
if (recognizer.state == UIGestureRecognizerStateRecognized)
{
CGRect viewRect = recognizer.view.bounds; // View bounds
CGPoint point = [recognizer locationInView:recognizer.view];
CGRect areaRect = CGRectInset(viewRect, TAP_AREA_SIZE, 0.0f); // Area
if (CGRectContainsPoint(areaRect, point)) // Single tap is inside the area
{
if ((m_CtrlViewTopBar.hidden == YES) || (m_CtrlViewBottomBar.hidden == YES))
{
[self showToolbar:m_CtrlViewTopBar];
[self showToolbar:m_CtrlViewBottomBar]; // Show
}
else
{
[self hideToolbar:m_CtrlViewTopBar];
[self hideToolbar:m_CtrlViewBottomBar];
}
return;
}
CGRect nextPageRect = viewRect;
nextPageRect.size.width = TAP_AREA_SIZE;
nextPageRect.origin.x = (viewRect.size.width - TAP_AREA_SIZE);
if (CGRectContainsPoint(nextPageRect, point)) // page++ area
{
//[self incrementPageNumber]; return;
}
CGRect prevPageRect = viewRect;
prevPageRect.size.width = TAP_AREA_SIZE;
if (CGRectContainsPoint(prevPageRect, point)) // page-- area
{
//[self decrementPageNumber]; return;
}
}
}
- (void)hideToolbar:(UIView*)view //Hide Toolbars
{
#ifdef DEBUGX
NSLog(#"%s", __FUNCTION__);
#endif
if (view.hidden == NO)
{
[UIView animateWithDuration:0.25 delay:0.0
options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction
animations:^(void)
{
view.alpha = 0.0f;
}
completion:^(BOOL finished)
{
view.hidden = YES;
}
];
}
[timer invalidate];
timer=nil;
}
- (void)showToolbar:(UIView*)view //Show Toolbars
{
#ifdef DEBUGX
NSLog(#"%s", __FUNCTION__);
#endif
if (view.hidden == YES)
{
[UIView animateWithDuration:0.25 delay:0.0
options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction
animations:^(void)
{
view.hidden = NO;
view.alpha = 1.0f;
}
completion:NULL
];
if (!timer) {
timer=[NSTimer scheduledTimerWithTimeInterval:5
target:self
selector:#selector(HideToolBarWithTime)
userInfo:nil
repeats:YES];
}
}
[self.view addSubview:view];
}
-(void)HideToolBarWithTime //Hide Toolbars with time
{
[self hideToolbar:m_CtrlViewTopBar];
[self hideToolbar:m_CtrlViewBottomBar];
[timer invalidate];
timer=nil;
}
// Gesture Delegates
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
return YES;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}