keyboard does not disappear from the view-iphone - 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.

Related

Keep UIScrollView in correct place after switching from landscape to portrait

I have a UIScrollView that contains a text field and a text view. I have code to move the text fields up when the keyboard is present so the keyboard does not cover the text fields. This code works great in portrait view:
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
if(textField) {
[textField resignFirstResponder];
}
return NO;
}
-(void)textFieldDidBeginEditing:(UITextField *)textField {
if (textField == self.nameField) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.3];
[UIView setAnimationBeginsFromCurrentState:YES];
self.view.frame = CGRectMake(self.view.frame.origin.x, (self.view.frame.origin.y - 90), self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
}
}
-(void)textFieldDidEndEditing:(UITextField *)textField {
if (textField == self.nameField) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.3];
[UIView setAnimationBeginsFromCurrentState:YES];
self.view.frame = CGRectMake(self.view.frame.origin.x, (self.view.frame.origin.y + 90), self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
}
}
-(void)textViewDidBeginEditing:(UITextView *)textField {
if (textField == self.questionField) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.3];
[UIView setAnimationBeginsFromCurrentState:YES];
self.view.frame = CGRectMake(self.view.frame.origin.x, (self.view.frame.origin.y - 200), self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
}
}
-(void)textViewShouldEndEditing:(UITextView *)textField {
if (textField == self.questionField) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.3];
[UIView setAnimationBeginsFromCurrentState:YES];
UIInterfaceOrientation interfaceOrientation = self.interfaceOrientation;
self.view.frame = CGRectMake(self.view.frame.origin.x, (self.view.frame.origin.y + 200), self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
}
}
When I rotate the iPhone simulator into a landscape view, this (not surprisingly) does not work. How can I get the text field to move up just enough to see it while typing into it in both landscape and portrait views?
Also, if I rotate from landscape to portrait while the keyboard is shown, after dismissing the keyboard the scroll view has moved down the screen instead of lined up in its original position. How can I avoid this?
Apple's document Managing the Keyboard shows the proper way to do this under the heading "Moving Content That Is Located Under the Keyboard".
You basically put your content on a scroll view and use the UIKeyboardDidShowNotification and UIKeyboardWillHideNotification notifications to adjust the content offsets.
However, the code is somewhat incomplete. The keyboard size calculation doesn't work in landscape mode. To fix it, replace this:
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
With this:
// Works in both portrait and landscape mode
CGRect kbRect = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
kbRect = [self.view convertRect:kbRect toView:nil];
CGSize kbSize = kbRect.size;

Keyboard hiding textfield animation for another View

When I press my textfield, the keyboard hides it. So I have implemented this code to "scroll" the view up:
- (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 = 80; // 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];
}
The UIView I'm trying to do this on is in another nib. I think since I have two UIViews, the program is getting confused and doesn't know which one to set the animation to.
I don't get any errors, but the animation isn't happening.
I've declared the UIView "surveyPage" for the view I'm trying to animate. Is there somewhere in the code above where I have to specify the UIView I want to animate?
Update:
The suggestion below didn't work for me. I tried changing self.view to surveyPage.
Above the code I posted above, I have this in my program:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}
The above code works without a problem, yet the animation does not.
You're specifying the UIView here:
self.view.frame = CGRectOffset(self.view.frame, 0, movement);
it's self.view.
Try:
[UIView animateWithDuration:movementDuration animations:^{
self.view.frame = CGRectOffset(self.view.frame, 0, movement);
}];
instead of
[UIView beginAnimations: #"anim" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
self.view.frame = CGRectOffset(self.view.frame, 0, movement);
[UIView commitAnimations];

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.

UITextView bad content offset after animating view

I have one UITextView which is on the bottom of view. When user tap in it, I need to animate view 150px up. I'm using:
- (void)textViewDidBeginEditing:(UITextView *)textView{
and
- (void)textViewDidEndEditing:(UITextView *)textView{
to make this.
On iOS5, it works fine. But on iOS 4.3, when view is animated, content in this UITextView goes few pixels up.
Screenshot: http://cl.ly/1q3e0W2G1M000u1U3w1f
Someone had similar issues?
Code:
- (void)textViewDidBeginEditing:(UITextView *)textView{
if([textView.text isEqualToString:#"Placeholder text"]){
textView.text = #"";
}
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.3];
[UIView setAnimationBeginsFromCurrentState:YES];
self.view.frame = CGRectMake(self.view.frame.origin.x, (self.view.frame.origin.y - 150.0), self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
}
- (void)textViewDidEndEditing:(UITextView *)textView{
if([textView.text isEqualToString:#""]){
textView.text = #"Placeholder text";
}
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.3];
[UIView setAnimationBeginsFromCurrentState:YES];
self.view.frame = CGRectMake(self.view.frame.origin.x, (self.view.frame.origin.y + 150.0), self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
}
You need reset content inset of your text view when call method in textViewDidBeginEditing method and after you animation is done...
Try this code:
- (void)textViewDidBeginEditing:(UITextView *)textView{
[textView setContentInset:UIEdgeInsetsMake(0, 0, 0, 0)];
}
I found that the accepted answer caused some visual irregularities. The following worked much better for me.
textView.contentMode = UIViewContentModeTop;
Try UITextField to solve this problem.

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;