Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move UIView up when the keyboard appears in iOS

I have a UIView, it is not inside UIScrollView. I would like to move up my View when the keyboard appears. Before I tried to use this solution: How can I make a UITextField move up when the keyboard is present?.

It was working fine. But after inserting the data in the textfields, it takes me to another view, and when I get back to this page, its just jumping, and I couldn't see my text fields. Is there any better solution for this problem?

like image 927
Anatoliy Gatt Avatar asked Jul 01 '12 13:07

Anatoliy Gatt


People also ask

How do I scroll up when keyboard appears in IOS?

There are two things to do in keyboardWasShown to scroll the text view. First, set the text view's content inset so the bottom edge is the keyboard's height. Second, set the scroll indicator insets to the text view's content inset.

How do I get the height of a swift key?

Swift. You can get the keyboard height by subscribing to the UIKeyboardWillShowNotification notification.


2 Answers

- (void)viewWillAppear:(BOOL)animated {     [super viewWillAppear: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 {     [super viewWillDisappear:animated];     [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];     [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; }  #pragma mark - keyboard movements - (void)keyboardWillShow:(NSNotification *)notification {     CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;      [UIView animateWithDuration:0.3 animations:^{         CGRect f = self.view.frame;         f.origin.y = -keyboardSize.height;         self.view.frame = f;     }]; }  -(void)keyboardWillHide:(NSNotification *)notification {     [UIView animateWithDuration:0.3 animations:^{         CGRect f = self.view.frame;         f.origin.y = 0.0f;         self.view.frame = f;     }]; } 
like image 65
theDuncs Avatar answered Sep 18 '22 18:09

theDuncs


Use the following code in order to show and hide the keyboard

//Declare a delegate, assign your textField to the delegate and then include these methods  -(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];     return YES; }   - (BOOL)textFieldShouldEndEditing:(UITextField *)textField {     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];      [self.view endEditing:YES];     return YES; }   - (void)keyboardDidShow:(NSNotification *)notification {     // Assign new frame to your view      [self.view setFrame:CGRectMake(0,-110,320,460)]; //here taken -110 for example i.e. your view will be scrolled to -110. change its value according to your requirement.  }  -(void)keyboardDidHide:(NSNotification *)notification {     [self.view setFrame:CGRectMake(0,0,320,460)]; } 
like image 45
Ani Shroff Avatar answered Sep 18 '22 18:09

Ani Shroff