0

Ok so I have gone through almost all questions on SO and tutorials. I am now able to move my UIView up so that it is visible while typing. However the problem is, I want to move the UIView ONLY if the UITextField gets covered by the keyboard, because if it is not, then there is no point in moving the UIView. My code so far:

- (void)viewDidLoad 
{
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self                                                selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 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;
    }];
}

Any help will be appreciated, thank you.

Fabio Berger
  • 1,921
  • 2
  • 24
  • 29
  • are you struggling with text filed and Keyboard? use https://github.com/michaeltyson/TPKeyboardAvoiding – Nitin Gohel Mar 01 '16 at 11:57
  • 1
    @TKutal If you are using autolayout than this link might be helpful to you .. http://stackoverflow.com/questions/31356293/uitableview-and-uiview-with-keyboardwillshow/31356527#31356527 – Bhavin Bhadani Mar 01 '16 at 12:07

1 Answers1

1

To check is UITextField covered by keyboard you can do something like this:

- (void)keyboardWillShow:(NSNotification *)notification {
    CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    if (myTextField.frame.origin.y + myTextField.frame.size.height > [UIScreen mainScreen].bounds.size.height - keyboardSize.height) {
        // textfield is covered by keyboard
        [UIView animateWithDuration:0.3 animations:^{
            CGRect f = self.view.frame;
            f.origin.y = -keyboardSize.height;
            self.view.frame = f;
        }];
    }
}
gvuksic
  • 2,983
  • 3
  • 32
  • 37
  • Can you help me with the 'y' position of the textField, the problem is if i add a constraint from the top of the screen the keyboard appears over the textfield, and if i add a bottom constraint the space between the keyboard and the textfield is same as that of the constraint. –  Mar 01 '16 at 12:23
  • also in the `if` condition how can I perform the check on all `UITextFields`, instead of just 'myTextField' –  Mar 01 '16 at 13:06
  • Anyone who wish to know more, check http://stackoverflow.com/questions/35738921/how-can-i-perform-a-check-on-all-check-uitextfields-in-if-condition/35739035?noredirect=1#comment59153925_35739035 –  Mar 02 '16 at 08:17