IPhone TextView hidden by keyboard - iphone

I have a UITextView editable when selected appears the keyboard. The problem is that when the text is so big he gets behind the keyboard. How can I solve this problem?

move your view up by this code
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
//change origin y
[UIView beginAnimations:nil context:self.view];
[UIView setAnimationDuration:0.25];
[self.view setFrame:CGRectMake(0,-150,320,436)];
[UIView commitAnimations];
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
//reset origin y
[UIView beginAnimations:nil context:self.view];
[UIView setAnimationDuration:0.25];
[self.view setFrame:CGRectMake(0,0,320,436)];
[UIView commitAnimations];
}
set x and y value according your requirement.

change base view size when edit UITextField,decrease base view orgin origin y to edit text field
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
//change origin y
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
//reset origin y
}

Use the delegate methods of UITextfield.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
[UIView beginAnimations:nil context:self.view];
[UIView setAnimationDuration:duration];
//change origin y
[self.view setFrame:CGRectMake(X,newY,width,height)];
[UIView commitAnimations];
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
[UIView beginAnimations:nil context:self.view];
[UIView setAnimationDuration:duration];
//reset origin y
[self.view setFrame:CGRectMake(X,Y,width,height)];
[UIView commitAnimations];
}
Don't forget to set the UITextFieldDelegate in your ".h" file and the appropriate text fileds textfiled.delegate = self.

Related

UIView animation not working on text begin editing in iphone app

I am making the iPad app in which I want to show animation when I start click on beginEditing here is the my code.
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
NSLog(#"This is calling");
[self showAnimationPests];
}
-(void) showAnimationPests
{
[UIView animateWithDuration:0.5
animations:^{
subView.frame = CGRectMake(0,-200,1024,748);
}];
}
It shows Log this is calling but view does not move.
If it's an iphone app why is the size of it 1024x748?
CGRectMake takes points not pixels (if you are trying to make up for retina display). And those points for iPhone 5 are 320x568 and previous devices 320x480.
try with my bellow code
Example..
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
subView.frame = CGRectMake(0,-200,1024,748);
[UIView commitAnimations];
return YES;
}
and set to 0 like default in textFieldShouldReturn: method like bellow..
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
subView.frame = CGRectMake(0,0,1024,748);
[UIView commitAnimations];
return YES;
}

How to Move View up when Click on UITextField in CustomCell of UITableview?

I have TextField in Custom Cell of UITableview .my Tableview is lower part of MainView so When i Click on TextField in CustomCell keyboard appear Which HIde my textfield.My below image show my problem
Here i have a Code which works when i have UITextField in MainView.please edit the Below Code Beacuse i want whole view Animation when i click on UITextfield.
-(void)textFieldDidBeginEditing:(UITextField *)textField {
if (textField.tag > 0) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.5];
[UIView setAnimationBeginsFromCurrentState:YES];
self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y-165.0,
self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
}
}
-(void)textresign{
[[self.view viewWithTag:1]resignFirstResponder];
}
-(void)textFieldDidEndEditing:(UITextField *)textField {
if (textField.tag > 0) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.5];
[UIView setAnimationBeginsFromCurrentState:YES];
self.view .frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y+165.0,
self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
}
}
But i did't know how to Fix it for my Custom cell UITextField.Any help will be appriated.
here is the code ,just need to change code little bit
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
if (textField.tag > 0)
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.5];
[UIView setAnimationBeginsFromCurrentState:YES];
self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y-165.0,
self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
}
}
use below method for repositioning the view on keyboard resigning instead of textFieldDidEndEditing.
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if (textField.tag > 0)
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.5];
[UIView setAnimationBeginsFromCurrentState:YES];
self.view .frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y+165.0,
self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
}
[textField resignFirstResponder];
return YES;
}
using below method it mandatory to set the delegate to the textfield.
i hope you could get the solution.

Moving Text Fields during input

I have set up some text fields and labels to look like a simple form which will have to get some user input. My problem is however that whenever some of the text fields are selected and the keyboard slides out, the text fields are covered making it impossible to see the input. My question is how can I move up the text fields so that they stay in view when selected. Maybe someone who is more experienced can help me on this.
A good solution for this which I like to do is listen to when the textfield begins editing with the following delegate function:
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
//keep a member variable to store where the textField started
_yPositionStore = textField.frame.origin.y;
//If we begin editing on the text field we need to move it up to make sure we can still
//see it when the keyboard is visible.
//
//I am adding an animation to make this look better
[UIView beginAnimations:#"Animate Text Field Up" context:nil];
[UIView setAnimationDuration:.3];
[UIView setAnimationBeginsFromCurrentState:YES];
commentTextField.frame = CGRectMake(commentTextField.frame.origin.x,
160 , //this is just a number to put it above the keyboard
commentTextField.frame.size.width,
commentTextField.frame.size.height);
[UIView commitAnimations];
}
and then in this callback you can return it to its original location:
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[UIView beginAnimations:#"Animate Text Field Up" context:nil];
[UIView setAnimationDuration:.3];
[UIView setAnimationBeginsFromCurrentState:YES];
commentTextField.frame = CGRectMake(commentTextField.frame.origin.x,
_yPositionStore ,
commentTextField.frame.size.width,
commentTextField.frame.size.height);
[UIView commitAnimations];
}
}
You will need to declare _yPositionStore as a member variable of CGFloat and have your textFields delegate be set to the view controller that owns it(either using Interface builder and dragging delegate to files owner or setting yourTextField.delegate = self)
I hope that helps
Do do this you need to follow next 2 steps:
- put all your UITextViews into UIScrollView
- register for keyboard notifications and slide your UIScrollView when keyboard appears
Both steps explained here: "Moving Content That Is Located Under the Keyboard"
You can call delegate methods of UITextfield and customize it according to requirement.
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[self animateTextField: textField up: YES];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[self animateTextField: textField up: NO];
}
- (void) animateTextField: (UITextField*) textField up: (BOOL) up
{
const int movementDistance = 110;
const float movementDuration = 0.3f;
int movement = (up ? movementDistance : -movementDistance);
[UIView beginAnimations: #"aimation" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
yourView.frame= CGRectOffset(yourView.frame,0, movement);
[UIView commitAnimations];
}
For my applications I use the following code assuming that keyboardHeight is 216 for portrait and 162 for landscape mode:
#pragma mark - textField delegate
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
// keyboard is visible, move views
CGRect myScreenRect = [[UIScreen mainScreen] bounds];
int keyboardHeight = 216;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.35];
int needToMove = 0;
CGRect frame = self.view.frame;
if (textField.frame.origin.y + textField.frame.size.height + self.navigationController.navigationBar.frame.size.height + [UIApplication sharedApplication].statusBarFrame.size.height > (myScreenRect.size.height - keyboardHeight)) {
needToMove = (textField.frame.origin.y + textField.frame.size.height + self.navigationController.navigationBar.frame.size.height + [UIApplication sharedApplication].statusBarFrame.size.height) - (myScreenRect.size.height - keyboardHeight);
}
frame.origin.y = -needToMove;
[self.view setFrame:frame];
[UIView commitAnimations];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
// resign first responder, hide keyboard, move views
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.35];
CGRect frame = self.view.frame;
frame.origin.y = 0;
[self.view setFrame:frame];
[UIView commitAnimations];
}
This formula takes into account the status bar, navigation bar and display size. And of course do not forget to set the delegate for your fields.
And with:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[[UIApplication sharedApplication] sendAction:#selector(resignFirstResponder) to:nil from:nil forEvent:nil];
}
you can hide the keyboard, when the user tap outside the field.

keyboard does not disappear from the view-iphone

I am havng one textfield and two textViews...while writting something in 1 conrol the keyboard is poped up...but when I want to shift on another control my keyboard doesn't let me write as it covers the whole view...hw can I solve my problem?
you should move your view up, so that the keyboard doesnt cover the textfield/ textview. something like this...
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
if (textField == *textFieldName*)
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.5];
[UIView setAnimationBeginsFromCurrentState:YES];
self.view.frame = CGRectMake(self.view.frame.origin.x, (self.view.frame.origin.y - 65.0), self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
}
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
if (textField == *textFieldName*)
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.5];
[UIView setAnimationBeginsFromCurrentState:YES];
self.view.frame = CGRectMake(self.view.frame.origin.x, (self.view.frame.origin.y + 65.0), self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
}
}
and for the textView use:
- (void)textViewDidBeginEditing:(UITextView *)textView
and
- (void)textViewDidEndEditing:(UITextView *)textView
Use delegate functions provided for uitextview and uitextfiled ....
- (void) animateTextField: (UITextField*) textField up: (BOOL) up
{
const int movementDistance = 150; // tweak as needed
const float movementDuration = 0.3f; // tweak as needed
int movement = (up ? -movementDistance : movementDistance);
[UIView beginAnimations: #"anim" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
self.view.frame = CGRectOffset(self.view.frame, 0, movement);
[UIView commitAnimations];
}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
[self animateTextField: textField up: YES];
}
- (void)textViewDidEndEditing:(UITextView *)textView{
[self animateTextField: textField up: NO];
}
you can shift your whole view up or just shift the controls (text field and text view) up .. or make your view scrollable so the user can scroll down while the keyboard is visible.

Keyboard hides UITextField. How to solve this?

I have a textField which is at bottom of the screen. When I begin editing, the keyboard appears and hides the textField.
Can anyone suggest how to solve this?
Generally you would put your content in a scroll view and move your scroll view up when the keyboard appears. There are many tutorials out there so google them. Here is one of them.
You can use following code in two diffrent method, call it when editing began and end, to move your view up and down,(just change the valuew that is been subtracted in yourView.frame.origin.y-100 to adjust according to your need)
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.5];
yourView.frame = CGRectMake(yourView.frame.origin.x, yourView.frame.origin.y-100, yourView.frame.size.width, yourView.frame.size.height);
[UIView commitAnimations];
See this code snippet. And use one UIButton to dismiss UIKeyboard.
- (void) animateTextField: (UITextField *)textField up:(BOOL) up
{
const int movementDistance = 200;
const float movementDuration = 0.4f;
int movement = (up ? -movementDistance : movementDistance);
up ? (self.button.enabled= YES) : (self.button.enabled = NO);
[UIView beginAnimations: #"anim" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
self.view.frame = CGRectOffset(self.view.frame, 0, movement);
[UIView commitAnimations];
}
-(void)textFieldDidBeginEditing:(UITextField*) inTextField
{
[self animateTextField:mText up:YES];
[self addObservers];
}
- (void)addObservers
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(textFieldDidChange:) name:#"UITextFieldTextDidChangeNotification" object:nil];
}
- (void)textFieldDidChange:(NSNotification*)aNotification
{
NSLog(#"TextField data=%#",mText.text);
}
- (IBAction) doneBtn:(id)sender
{
[mText resignFirstResponder ];
[self animateTextField:mText up:NO];
}
When you tap on textField keyboard up then your textField hide by it. so waht you need, you need to make your view up not keyboardresign is a solution. so for making you view up use
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField delegate for the logic. now see the code
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if(textField.tag==1) //add tag to your textField to identify it
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.3];
CGRect viewFrame=self.view.frame;
viewFrame=sinViewFrame.size.height+100;
viewFrameorigin.y-=100; // as required
self.view.frame=viewFrame;
[UIView commitAnimations];
}
}
hope it helps you.
I use auto layout constraint to solve this problem. I hold constraint as a property, and play with it when keyboard appears / disappears. I simply move views above the textfield out of screen, and textfield to the top. After finishing edit, I move them by the constraint to their original position.
-(void)keyboardWillShow:(NSNotification*)notification{
[UIView animateWithDuration:0.25 delay:0.0 options:UIViewAnimationOptionTransitionNone animations:^{
self.topSpaceLayoutConstraint.constant = -150;
[self.view setNeedsLayout];
[self.view layoutIfNeeded];
} completion:NULL];
}
-(void)keyboardWillHide:(NSNotification*)notification
{
[UIView animateWithDuration:0.25 delay:0.0 options:UIViewAnimationOptionTransitionNone animations:^{
self.topSpaceLayoutConstraint.constant = 10;
[self.view setNeedsLayout];
[self.view layoutIfNeeded];
} completion:NULL];
}
#property (weak, nonatomic) IBOutlet NSLayoutConstraint *topSpaceLayoutConstraint;