0

Apologies for this basic question, I am new to iOS development. I have a UITextField in a View with AutoLayout that I would like to use to key in messages for a chat system. However when the keypad is displayed it hides the View containing the UITextField (the entire View is behind the keypad).

What should be done to move the View along with the keypad when the keypad transitions from the bottom? The UIView should be back in its original position (at the bottom of the screen) when the keypad is dismissed. My entire UI has been designed with AutoLayout in Storyboard.

Edit: I looked up How do I scroll the UIScrollView when the keyboard appears? for a solution however there doesn't seem to be any indication AutoLayout has been used along constraints in this answer. How can the same be achieved using AutoLayout in Storyboard. Again, apologies for any lack of understanding as I am very new to iOS development.

Community
  • 1
  • 1
user1841702
  • 2,163
  • 4
  • 30
  • 49
  • http://stackoverflow.com/questions/13161666/how-do-i-scroll-the-uiscrollview-when-the-keyboard-appears read here – Blind Ninja Sep 16 '15 at 12:32

2 Answers2

0

Add a UIScrollView to your view controller and keep your UITextField over scrollview.

add UITextFieldDelegate

yourTextField.delegate = self;

you can set content offset of scrollview when touch on UITextField and reposition it to (0, 0) when keyboard resign.

-(void)viewWillAppear:(BOOL)animated
{
yourScrollView.contentSize = CGSizeMake(320, 500);

[super viewWillAppear:YES];

}

-(void)textFieldDidBeginEditing:(FMTextField *)textField
{
    [yourScrollView setContentOffset:CGPointMake(0,textField.center.y-140) animated:YES];//you can set your  y cordinate as your req also
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
     [textField resignFirstResponder];
     [yourScrollView setContentOffset:CGPointMake(0,0) animated:YES];
    return YES;
} 
Bhanu
  • 1,211
  • 9
  • 17
0

This is what I did recently, may be it helps.

All my fields are inside a wrapper view with a top constraint. Since for me moving the wrapper view a few pixels up and down was enough I use this approach. Here is an example with a scroll view.

I use a IBOutlet to reference this constraint

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topConstraint;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //register for keyboard notifications
    _keyboardIsShowing = NO;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

Then on the notification methods

  #pragma mark - Keyboard notifications methods
    - (void)keyboardWillShow:(NSNotification *) notification{

        if (!_keyboardIsShowing) {
            _keyboardIsShowing = YES;
            [UIView animateWithDuration:0.4 animations:^{
//here update the top constraint to some new value
                _topConstraint.constant = _topConstraint.constant - 30;
                [self.view layoutIfNeeded];
            }];
        }
    }

    - (void)keyboardWillHide:(NSNotification *) notification{
        if (_keyboardIsShowing) {
            [UIView animateWithDuration:0.4 animations:^{
                _topConstraint.constant = _topConstraint.constant + 30;
                [self.view layoutIfNeeded];
            }];

            _keyboardIsShowing = NO;
        }
    }

There are tons of this kind of answers here on SO. Good luck.

Lucho
  • 949
  • 13
  • 20