UIScrollView and multiple UITextField - iphone

I have a little problem with uitextfield & uiscrollview, the problem is that when I click on uitextfield the keyboard hide the field, for solve this I use:
- (BOOL) textFieldShouldBeginEditing:(UITextField *)textField {
CGRect viewFrame = self.scroll.frame;
viewFrame.size.height -= 186;
[self.scroll setFrame:viewFrame];
return YES;
}
- (BOOL) textFieldShouldReturn:(UITextField *)textField{
[self.scroll setFrame:CGRectMake(0, 0, 320, 480)];
[self inserisciField];
[textField resignFirstResponder];
return YES;
}
and all work, the problem is with multiple field, with a field selected if I change the field without click return the scroll view has a wrong size/positon but if I click return, then I Select a field, press return, change field, ecc...all work.
I think that there is this problem because If I change field the scroll dimension was changed two time (the first time view frame.size.height -= 186, and another time with -=186) then I try to put this method
- (BOOL) textFieldShouldEndEditing:(UITextField *)textField {
[self.scroll setFrame:CGRectMake(0, 0, 320, 480)];
return YES;
}
where I set the default size of frame, but nothing.

use this -
- (BOOL) textFieldShouldBeginEditing:(UITextField *)textField {
CGPoint point = CGPointMake(scroll.frame.origin.x, scroll.frame.origin.y + 186);
[self.scroll setContentOffset:point];
return YES;
}
- (BOOL) textFieldShouldEndEditing:(UITextField *)textField {
CGPoint point = CGPointMake(0, 0);
[self.scroll setContentOffset:point];
}

You can use the contentoffset property of scrollview when u touch or edit a textfield as it is the best solution for your problem and touch and edit are the same process - only when you touch the field, it becomes editable.

Related

Textfield inside the scrollView iPhone

I am new to iPhone, I have 10 textfields in my application which are in a scrollview.
What i need is when user touches on a textfield,scrollview should scroll in such a way, so that textfield should not be behind the keyboard.
Help me.
Thanks for your kind help to me.
override the textfield delegate methods like this
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[self scrollViewToCenterOfScreen:textField];
}
//This method moves the current selected text field to the visible zone
-(void)scrollViewToCenterOfScreen:(UIView *)theView
{
CGFloat viewCenterY = theView.center.y;
CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
CGFloat availableHeight = applicationFrame.size.height - 200; // Remove area covered by keyboard  
CGFloat y = viewCenterY - availableHeight / 2.0;
if (y < 0) {
y = 0;
}
[scrollview setContentOffset:CGPointMake(0, y) animated:YES];
}
And when you want to dismiss the keyboard override this delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
[self.scrollview setContentOffset:CGPointMake(0, 0) animated:YES];
return YES;
}

Keyboard hides the custom cell

I am having a custom cell in my tableview, when showing keyboard some of my cell gets hidden behind the keyboard.
To fix this issue I have tried as below
- (void) textFieldDidBeginEditing:(UITextField *)textField {
CustomCell *cell = (CustomCell*) [[textField superview] superview];
NSIndexPath *indexPath = [self.mySmlMsgTemplatesTbl indexPathForCell:cell];
[self.mySmlMsgTemplatesTbl scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
// [self.mySmlMsgTemplatesTbl scrollToRowAtIndexPath:[self.mySmlMsgTemplatesTbl indexPathForCell:cell] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
But it seems to be not working.
You should try following code as I have shown below. I didn't try it but it should work.
- (void)textFieldDidBeginEditing:(UITextField *)textField{
CGPoint point = [self.tableView convertPoint:yourtextview.bounds.origin fromView:yourtextview];
NSIndexPath* path = [self.tableView indexPathForRowAtPoint:point];
[self.tableView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
If it is the last cell for example, this won't work. The tableview can't scroll that far up. You will need to resize the tableview frame or use it's contentInset property to resize it.
There is another way. You could try to move the whole view up and down.
Hope this will help
#define kOFFSET_FOR_KEYBOARD 200.0
- (BOOL)textFieldShouldBeginEditing:(UITextField*)textField {
[txtField addTarget:self action:#selector(setViewMovedUp:)forControlEvents:UIControlEventEditingDidBegin];
[txtField addTarget:self action:#selector(setViewMovedDown:)forControlEvents:UIControlEventEditingDidEndOnExit];
}
-(void)setViewMovedUp:(id)sender
{
if (isKeyboardAppeared) {
return;
}
isKeyboardAppeared = YES;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3]; // if you want to slide up the view
CGRect rect = self.view.frame;
rect.origin.y -= kOFFSET_FOR_KEYBOARD;
rect.size.height += kOFFSET_FOR_KEYBOARD;
self.view.frame = rect;
[UIView commitAnimations];
}
-(void)setViewMovedDown:(id)sender
{
[self actionSaveRegistration];
if (!isKeyboardAppeared) {
return;
}
isKeyboardAppeared = NO;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
CGRect rect = self.view.frame;
rect.origin.y += kOFFSET_FOR_KEYBOARD;
rect.size.height -= kOFFSET_FOR_KEYBOARD;
self.view.frame = rect;
[UIView commitAnimations];
}
i do this type of app you just need the textFieldname or textfield tag ..and you can pgive the tag to textField with visiblecell...
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if(textField.tag==3)
{
tableview.frame=CGRectMake(tableview.frame.origin.x, tableview.frame.origin.y-40,tableview.frame.size.width , tableview.frame.size.height+40);
}
else if(textField.tag==4)
{
tableview.frame=CGRectMake(tableview.frame.origin.x, tableview.frame.origin.y-40,tableview.frame.size.width , tableview.frame.size.height+40);
}
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if(textField.tag==3)
{
tableview.frame=CGRectMake(0,0, 320,460);
//tableview.frame=CGRectMake(tableview.frame.origin.x, tableview.frame.origin.y+70,tableview.frame.size.width , tableview.frame.size.height-70);
}
else if(textField.tag==4)
{
tableview.frame=CGRectMake(0,0, 320,460);
//tableview.frame=CGRectMake(tableview.frame.origin.x, tableview.frame.origin.y+70,tableview.frame.size.width , tableview.frame.size.height-70);
}
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[textField resignFirstResponder];
if(textField.tag==3)
{
tableview.frame=CGRectMake(0,0, 320,460);
//scrollview.frame=CGRectMake(tableview.frame.origin.x, tableview.frame.origin.y+70,tableview.frame.size.width , tableview.frame.size.height-70);
}
else if(textField.tag==4)
{
tableview.frame=CGRectMake(0,0, 320,460);
//tableview.frame=CGRectMake(scrollview.frame.origin.x, tableview.frame.origin.y-70,tableview.frame.size.width , tableview.frame.size.height+70);
}
}
i use here scrollView for Registration form here not a perfect code which you want but i think you can get idea from this code...
Hope,this help you..
:)

how to stretch a UITextField on begin editng

i want to increase the size of UITextField when editing begins, but just cant get the code to do it.
Gomathi is correct. Just to add to his answer if you want the textFIeld to expand when editing begins and shrink back again when editing ends and also want to animate it, you might wanna do something like this:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
[UIView animateWithDuration:0.5 animations:^{
textField.frame = CGRectMake(textField.frame.origin.x-20, textField.frame.origin.y, textField.frame.size.width+40, textField.frame.size.height);
}];
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[UIView animateWithDuration:0.5 animations:^{
textField.frame = CGRectMake(textField.frame.origin.x+20, textField.frame.origin.y, textField.frame.size.width-40, textField.frame.size.height);
}];
[textField resignFirstResponder];
return YES;
}
Implement UITextFieldDelegate.
In - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField, set the new frame size to the textfield. Then call, [textField setNeedsDisplay];.

How to hide keybord while typing in iphone app

I have 2 textview one is on top and other is at bottom. When I am trying to write something on bottom textview keyboard is coming on screen and I am not able to see what I am typing.
How to make keyboard invisible or placed on perfect place while typing below textview?
- (void)textViewDidBeginEditing:(UITextView *)textView {
if (textView == bottomTextView) {
CGRect frame = self.view.frame;
frame.origin.y -= 250;
self.view.frame = frame;
}
}
- (void)textViewDidEndEditing:(UITextView *)textView {
if (textView == bottomTextView) {
CGRect frame = self.view.frame;
frame.origin.y += 250;
self.view.frame = frame;
}
}
You can modify this 250 value.
The Answer to your problem lies here buddy..
Sliding UITextFeilds when keyboard appears
Its Best solution available.
Write This Delegate Methods For TextField
- (void)textFieldDidBeginEditing:(UITextField *)textField {
if (textField == txtUserName) {
[txtUserName becomeFirstResponder];
[scrollView setContentOffset:CGPointMake(0,45) animated:YES];
} else if (textField == txtPassword) {
[txtPassword becomeFirstResponder];
[scrollView setContentOffset:CGPointMake(0,105) animated:YES];
}
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
[scrollView setContentOffset:CGPointMake(0,0) animated:YES];
return YES;
}

Help me on UITextField

i have list of UITextFields.
i want to move to the next text fields one by one after entering input in it.
since the keyboard is hiding the fields....
can you people show me that how can i accomplish that...
Thank a lot for your time.
(void)shiftViewUp:(BOOL)up
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
CGRect rect = self.view.frame;
if (movedUp)
{
if(rect.origin.y == 0){
rect.origin.y -= 85.0;
rect.size.height += 85.0;
}
}
else
{
if(rect.origin.y != 0.0){
rect.origin.y += 85.0;
rect.size.height -= 85.0;
}
}
self.view.frame = rect;
[UIView commitAnimations];
}
Basically you can call this method with BOOL YES when you begin editing the textfiled & with BOOL no when you end editing.
You just need to track which textfield is editing based on that you can adjust shifting (eg 85 here)
An alternative approach could be to use a UITableView. Then most of the controls are ready. But you can also make it all happen by yourself.
First put your UITextfields on a separate UIView. Make that UIView an outlet en link it properly. That way you can easily move the textfields up with an animation.
Then do something like this in the delegate methods:
// Sets the label of the keyboard's return key to 'Done' when the insertion
// point moves to the table view's last field.
//
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if ([textField tag] == myLastUITextField)
{
[textField setReturnKeyType:UIReturnKeyDone];
}
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if ([textField returnKeyType] != UIReturnKeyDone)
{
// If this is not the last field (in which case the keyboard's
// return key label will currently be 'Next' rather than 'Done'),
// just move the insertion point to the next field.
//
NSInteger nextTag = [textField tag] + 1;
UIView *nextTextField = [myTextView viewWithTag:nextTag];
// here starts your animation code
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
CGRect rect = textFieldView.frame;
rect.origin.y = -44;
textFieldView.frame = rect;
[UIView commitAnimations];
// here ends your animation code. Play with it
[nextTextField becomeFirstResponder];
}
else
{
// do what you want to do with the last UITextfield
}
return YES;
}
`
I think it will not immediately work by copy-pasting but I hope it points in a direction. Good luck