Ios moves scrollview when keyboard is above text box

please, could you fix my code, so I have an example of a simple application with a text field that moves up when the keyboard is above it?

I tried to implement it using the code from the ios developers library https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html

but I don’t know what they mean by “The active field is stored in a user variable (called activeField in this example),” and I probably have something wrong. Using registerForKeyboardNotifications in viewWillAppear is ok?

I know that there are some topics about this problem, but I am a beginner and it is difficult for me to understand them. And I don’t want to just learn, but why, why I don’t want to use a ready-to-use solution from other github, etc.

My atm code is:

.h:

#import <UIKit/UIKit.h> @interface VNViewController : UIViewController<UIScrollViewDelegate, UITextFieldDelegate> @property (weak, nonatomic) IBOutlet UITextField *texticek; @property (strong, nonatomic) IBOutlet UIScrollView *scrollView; @end 

.m:

 #import "VNViewController.h" @interface VNViewController () @end @implementation VNViewController @synthesize scrollView; @synthesize texticek; - (void) viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self registerForKeyboardNotifications]; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } // Call this method somewhere in your view controller setup code. - (void)registerForKeyboardNotifications { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil]; } // Called when the UIKeyboardDidShowNotification is sent. - (void)keyboardWasShown:(NSNotification*)aNotification { NSDictionary* info = [aNotification userInfo]; CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0); scrollView.contentInset = contentInsets; scrollView.scrollIndicatorInsets = contentInsets; // If active text field is hidden by keyboard, scroll it so it visible // Your app might not need or want this behavior. CGRect aRect = self.view.frame; aRect.size.height -= kbSize.height; if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) { [self.scrollView scrollRectToVisible:activeField.frame animated:YES]; } } // Called when the UIKeyboardWillHideNotification is sent - (void)keyboardWillBeHidden:(NSNotification*)aNotification { UIEdgeInsets contentInsets = UIEdgeInsetsZero; scrollView.contentInset = contentInsets; scrollView.scrollIndicatorInsets = contentInsets; } - (void)textFieldDidBeginEditing:(UITextField *)textField { activeField = textField; } - (void)textFieldDidEndEditing:(UITextField *)textField { activeField = nil; } @end 
+6
source share
7 answers

Moving a text field when you click on it, use the code below. it only needs to exit your scroll

 - (void)textFieldDidBeginEditing:(UITextField *)textField { self.scroll.contentOffset = CGPointMake(0, textField.frame.origin.y); } 

you can change the position of the text field where the minus value of the y position should be displayed (textfield.frame.origin.y - some value)

If you want to revive the scroll, you can do it like this:

 CGPoint newOffset = CGPointMake(0, textField.frame.origin.y-40); [self.scroll setContentOffset: newOffset animated: YES]; 
+31
source

In the ViewDidLoad register notifications:

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil]; 

Add the observer methods below that automatically scroll when the keyboard appears.

 - (void)keyboardWasShown:(NSNotification*)notification { NSDictionary *info = [notification userInfo]; CGRect keyboardRect = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue]; keyboardRect = [self.view convertRect:keyboardRect fromView:nil]; UIEdgeInsets contentInset = self.scrollView.contentInset; contentInset.bottom = keyboardRect.size.height; self.scrollView.contentInset = contentInset; } - (void)keyboardWillBeHidden:(NSNotification*)notification { UIEdgeInsets contentInsets = UIEdgeInsetsZero; self.scrollView.contentInset = contentInsets; } #pragma mark TextField Delegates - (BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; return YES; } 
+9
source

Here is a solution using Swift:

 import UIKit class ExampleViewController: UIViewController, UITextFieldDelegate { @IBOutlet var scrollView: UIScrollView! @IBOutlet var textField1: UITextField! @IBOutlet var textField2: UITextField! @IBOutlet var textField3: UITextField! @IBOutlet var textField4: UITextField! @IBOutlet var textField5: UITextField! var activeTextField: UITextField! // MARK: - View override func viewDidLoad() { super.viewDidLoad() self.textField1.delegate = self self.textField2.delegate = self self.textField3.delegate = self self.textField4.delegate = self self.textField5.delegate = self } override func viewWillAppear(animated: Bool) { super.viewWillAppear(animated) self.registerForKeyboardNotifications() } override func viewWillDisappear(animated: Bool) { super.viewWillDisappear(animated) self.unregisterFromKeyboardNotifications() } // MARK: - Keyboard // Call this method somewhere in your view controller setup code. func registerForKeyboardNotifications() { let center: NSNotificationCenter = NSNotificationCenter.defaultCenter() center.addObserver(self, selector: "keyboardWasShown:", name: UIKeyboardDidShowNotification, object: nil) center.addObserver(self, selector: "keyboardWillBeHidden:", name: UIKeyboardWillHideNotification, object: nil) } func unregisterFromKeyboardNotifications () { let center: NSNotificationCenter = NSNotificationCenter.defaultCenter() center.removeObserver(self, name: UIKeyboardDidShowNotification, object: nil) center.removeObserver(self, name: UIKeyboardWillHideNotification, object: nil) } // Called when the UIKeyboardDidShowNotification is sent. func keyboardWasShown (notification: NSNotification) { let info : NSDictionary = notification.userInfo! let kbSize = (info.objectForKey(UIKeyboardFrameBeginUserInfoKey)?.CGRectValue() as CGRect!).size let contentInsets: UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0); scrollView.contentInset = contentInsets; scrollView.scrollIndicatorInsets = contentInsets; // If active text field is hidden by keyboard, scroll it so it visible // Your app might not need or want this behavior. var aRect = self.view.frame aRect.size.height -= kbSize.height; if (!CGRectContainsPoint(aRect, self.activeTextField.frame.origin) ) { self.scrollView.scrollRectToVisible(self.activeTextField.frame, animated: true) } } // Called when the UIKeyboardWillHideNotification is sent func keyboardWillBeHidden (notification: NSNotification) { let contentInsets = UIEdgeInsetsZero; scrollView.contentInset = contentInsets; scrollView.scrollIndicatorInsets = contentInsets; } // MARK: - Text Field func textFieldDidBeginEditing(textField: UITextField) { self.activeTextField = textField } func textFieldDidEndEditing(textField: UITextField) { self.activeTextField = nil } } 
+6
source

BSKeyboardControl is a good control. You need to provide text fields in your view. Some of them implement the delegation method. Then it is too easy for that. The toolbar with the previous and next buttons is available for scrolling text fields. The following link will give you a detailed explanation of BSKeyboardControl

https://github.com/simonbs/BSKeyboardControls#usage

+1
source

Change one function:

 - (void)keyboardWasShown:(NSNotification*)aNotification { NSDictionary* info = [aNotification userInfo]; CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0); scrollView.contentInset = contentInsets; scrollView.scrollIndicatorInsets = contentInsets; // If active text field is hidden by keyboard, scroll it so it visible // Your app might not need or want this behavior. CGRect aRect = self.view.frame; aRect.size.height -= kbSize.height; if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) { [scrollView setContentOffset:CGPointMake(0,aRect.size.height) animated:YES]; }} 
+1
source
 -(void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; //添加键盘弹出通知[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(didChangeKeyBoardFrame:) name:UIKeyboardWillChangeFrameNotification object:nil]; } -(void)viewDidAppear:(BOOL)animated { //设置原始contentSize; [super viewDidAppear:animated]; originalSize = self.mscrollView.contentSize; } -(void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; //移除键盘弹出通知[[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillChangeFrameNotification object:nil]; } 

pragma sign - private

 -(void)didChangeKeyBoardFrame:(NSNotification *)sender { CGRect endRect = [[sender.userInfo objectForKey:@"UIKeyboardFrameEndUserInfoKey"] CGRectValue]; CGPoint mf =[self.view convertPoint:CGPointMake(0,self.name_textfieldDown.frameBottom) fromView:self.mscrollView]; //does the keyboard cover my label CGFloat distance = mf.y - endRect.origin.y; // it mean that keyboard will hidden if(endRect.origin.y == ScreenHeight) { // place scrollView back [UIView animateWithDuration:0.25 animations:^{ self.mscrollView.contentOffset = CGPointMake(0, self.mscrollView.contentSize.height- self.mscrollView.height); self.mscrollView.contentSize = originalSize; }]; } else if (distance>0) { // [UIView animateWithDuration:0.25 animations:^{ CGSize contentSize = originalSize; contentSize.height +=( endRect.size.height ) ;// I want users can drag scrollView to buttom when keyboard is showing self.mscrollView.contentSize = contentSize; self.mscrollView . contentOffset = CGPointMake(0, self.mscrollView.contentOffset.y + distance+20); }]; } } 
0
source

UITextField * activeField; for activeField you need to declare in a .m file

0
source

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


All Articles