Text field animation on click

I am trying to move a text box at the bottom of the screen in an iPad application so that the keyboard does not close it.

I have the following that works.

-(void) textViewDidBeginEditing:(UITextView *) observationComment { observationComment.frame=CGRectMake(190, 100, 700, 250); } 

But 1 - I would like to revive the movement - is this possible?

2 - I get a warning

 Local declaration of 'observationComment' hides instance variable 

Any tips? This may not be the best way to do this.

+1
source share
2 answers

Previously, I found the answer here , but I had to add code so that the View Viewer view.frame is configured when the user switches between keyboards (e.g. international or emoji) by pressing the globe key ( enter image description here )

In YourViewController.h, save the height of the keyboard (only when it is visible) in the instance variable.

 @interface YourViewController : UIViewController { CGFloat keyboardHeightIfShowing ; } 

In YourViewController.m implement these methods.

 - (void) keyboardWillShow:(NSNotification*)notification { [self moveTextViewForKeyboard:notification up:YES] ; } - (void) keyboardWillHide:(NSNotification*)notification { [self moveTextViewForKeyboard:notification up:NO] ; } - (void) moveTextViewForKeyboard:(NSNotification*)notification up:(BOOL)up { NSDictionary* userInfo = [notification userInfo] ; UIViewAnimationCurve animationCurve ; NSTimeInterval animationDuration ; CGRect keyboardEndFrame ; [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve] ; [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration] ; [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardEndFrame] ; [UIView beginAnimations:nil context:nil] ; [UIView setAnimationDuration:animationDuration] ; [UIView setAnimationCurve:animationCurve] ; CGRect keyboardFrame = [self.view convertRect:keyboardEndFrame toView:nil] ; //Since I have a UITabBar, when the keyboard appears, self.view.frame.height should shrink by slightly less than if I did not have a UITabBar. keyboardFrame.size.height -= self.tabBarController.tabBar.frame.size.height ; CGRect newViewFrame = self.view.frame ; //UIKeyboardWillShowNotification can be triggered even when the keyboard is already showing, such as when switching between certain international keyboards. When this happens, before shrinking newViewFrame to accommodate keyboardFrame, enlarge newViewFrame so that it is the size it was before the previous keyboard appeared. if ( up && keyboardHeightIfShowing ) { NSLog(@"hiding keyboard with height %0.1f, showing keyboard with height %0.1f",keyboardHeightIfShowing, keyboardFrame.size.height) ; newViewFrame.size.height += keyboardHeightIfShowing ; } newViewFrame.size.height -= keyboardFrame.size.height * (up ? 1 : -1) ; keyboardHeightIfShowing = ( up ? keyboardFrame.size.height : 0 ) ; self.view.frame = newViewFrame ; [UIView commitAnimations] ; } 

And finally, in YourViewController.m set keyboardWillShow and keyboardWillHide to call using UIKeyboardWillShow / Hide notifications.

 - (void) viewWillAppear:(BOOL)animated { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil] ; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil] ; } - (void) viewWillDisappear:(BOOL)animated { [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil] ; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil] ; } 
+1
source

Check out this tutorial or fooobar.com/questions/749 / .... This is a difficult problem, but it should give you a better idea of ​​what you need to do.

0
source

Source: https://habr.com/ru/post/973448/


All Articles