ruby_ruby ruby_ruby - 4 months ago 12
iOS Question

ios move scrollview when keyboard is over text field

please, could you correct my code, so i have an example of simple app with text field which moves up when keyboard is over it?

I tried to implement it using code from from ios developer 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 custom variable (called activeField in this example)" and I did probably something else wrong. Using registerForKeyboardNotifications in viewWillAppear is ok?

I know there are some threads about this problem, but I am newbie and it's hard to understand them for me. And I don't want to just learn how, but why, that's why I don't want to use ready to use solution from github others advised etc.

My code atm:

.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's 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

Answer

Moving up textfield while clicking it, use below code . it requires only outlet of your scroll

- (void)textFieldDidBeginEditing:(UITextField *)textField {

    self.scroll.contentOffset = CGPointMake(0, textField.frame.origin.y);
}

you can change the position of textfield where should appear by minus the y position value (textfield.frame.origin.y - some value)

If you want to animate the scroll you can do like this :

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