How to hide keybord while typing in iphone app - iphone

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;
}

Related

how can i scroll between many textfield and textarea on uiscrollview?

i have an nsarray of textfields and another of textarea. I have also a toolbar with next and previous button to manage navigation between my fields. I made a fonction in textfieldDidBeginEditing and another in textfieldDidEndEditing but it didn't work correctly.
my function is :
if (textField.frame.origin.y > 180) {
[UIScrollView beginAnimations:nil context:NULL];
[UIScrollView setAnimationDelegate:self];
[UIScrollView setAnimationDuration:0.4];
[UIScrollView setAnimationBeginsFromCurrentState:YES];
CGFloat updatedY = 150;
self.scrollView.frame = CGRectMake(self.scrollView.frame.origin.x,self.scrollView.frame.origin.y - updatedY, self.scrollView.frame.size.width,
self.scrollView.frame.size.height);
[UIScrollView commitAnimations];
}
Any help will be appreciated.
I think you want Keyboard handler. The one I am using is a very good keyboard handler.
Keyboard Handler by Sukhpal Singh
This is a very good tutorial from one of my good friend and You can use this.
For using this you just have to write a single line of code.
AutoScroller * scroller=[AutoScroller addAutoScrollTo:self.scrollView isDoneToolBarNeeded:NO];
yes its done.
Please try to use this one
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
[UIView beginAnimations:#"" context:nil];
[UIView setAnimationDuration:.30];
[self scrollViewToCenterOfScreen:textView];
[UIView commitAnimations];
return YES;
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
[UIView beginAnimations:#"" context:nil];
[UIView setAnimationDuration:.30];
[self scrollViewToCenterOfScreen:textField];
[UIView commitAnimations];
return YES;
}
- (void)scrollViewToCenterOfScreen:(UIView *)theView
{
CGFloat y ;
CGFloat viewCenterY = theView.center.y;
CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
CGFloat avaliableHeight = applicationFrame.size.height - 250;
y = viewCenterY - avaliableHeight / 2.0f;
if (y < 0)
{
y = 0;
}
[scrollView setContentOffset:CGPointMake(0, y)];
}
try this code:
- (void)textFieldDidBeginEditing:(UITextField *)textField;
{
[clientscrollview setContentOffset:CGPointMake(0, textField.center.y-180) animated:YES];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[clientscrollview setContentOffset:CGPointMake(0, 0) animated:YES];
[textField resignFirstResponder];
return YES;
}
- (void)textViewDidBeginEditing:(UITextView *)textView
{
[clientscrollview setContentOffset:CGPointMake(0,textView.center.y-180) animated:YES];
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
[clientscrollview resignFirstResponder];
[clientscrollview setContentOffset:CGPointMake(0,0) animated:YES];
}

Back ground resizing on keyboard visiblity

I'm using scroll view in my application ,since when i click on dob text-field the datepicker view is showing as a pop up ,on further when i click in continuous text field the view is being like in the image,Here my code,
For date picker visibility.
UIDatePicker pop up after UIButton is pressed
For keyboard orientation
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[dob resignFirstResponder];
if (txt1.textColor == [UIColor lightGrayColor]) {
txt1.text = #"";
txt1.textColor = [UIColor blackColor];
}
if ([textField isEqual:dob])
{
[self but];
[dob resignFirstResponder];
//return NO;
}
//[self animateTextField:textField up:YES];
[textField setClearButtonMode:UITextFieldViewModeWhileEditing];
CGRect textFieldRect = [self.view.window convertRect:textField.bounds fromView:textField];
CGRect viewRect = [self.view.window convertRect:self.view.bounds fromView:self.view];
CGFloat midline = textFieldRect.origin.y + 0.1 * textFieldRect.size.height;
CGFloat numerator = midline - viewRect.origin.y - MINIMUM_SCROLL_FRACTION * viewRect.size.height;
CGFloat denominator = (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION) * viewRect.size.height;
CGFloat heightFraction = numerator / denominator;
if (heightFraction < 0.0)
{
heightFraction = 0.0;
}
else if (heightFraction > 1.0)
{
heightFraction = 1.0;
}
UIInterfaceOrientation orientation =
[[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait ||
orientation == UIInterfaceOrientationPortraitUpsideDown)
{
animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
}
else
{
animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
}
CGRect viewFrame = self.view.frame;
viewFrame.origin.y -= animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
//[self animateTextField:textField up:NO];
CGRect viewFrame = self.view.frame;
viewFrame.origin.y += animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
return [textField resignFirstResponder];
return [txt1 resignFirstResponder];
}
Can any one help me to clear.
You need to resize your scrollview when the key board appears.
this can be done by adding:
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self selector:#selector(keyboardShown) name:UIKeyboardDidShowNotification object:nil];
[center addObserver:self selector:#selector(keyboardHidden) name:UIKeyboardWillHideNotification object:nil];
in the viewDidLoad.
Then define the methods keyboardShown and keyboardHidden in your implementation file.
These methods will be called automatically when keyboard appears and disappears.
In these methods resize your background view accordingly. since you are using scroll view please mind the scrollView's contectView size also. as its different from the view's frame size.
For the basic problem of having a scroll view and needing to move its contents when the keyboard appears, I have found this open source scroll view class by Michael Tyson to be very useful.
Basically, you just add the TPKeyboardAvoidingScrollView class to your project, and use it where you would normally use a UIScrollView. The child views that you add to the TPKeyboardAvoidingScrollView can be UITextField objects, if you like. When you tap those fields to begin editing, and the keyboard appears, the TPKeyboardAvoidingScrollView container will choose an appropriate scroll position so that the field that's being edited is not hidden by the keyboard, and you won't see black bars like you have in your screen shot.
I think i get the problem.
jst for example, in ur register page, many textfields and button for DOB.
So, basically when any textfield editing and after that instead of return keyboard, user press button of DOB.
so u have to first resignFirstRespoder ur all textfields.
Problem is when u startEditing frame set to top and endEditing set to bottom but when u click to button endEditing not called so frame not set to bottom. And again u startEditing frame again set to top so problem aries.
I hope this help...

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..
:)

UIScrollView and multiple UITextField

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.