Skip to content

Instantly share code, notes, and snippets.

@anderssvendal
Created July 5, 2012 09:50
Show Gist options
  • Select an option

  • Save anderssvendal/3052679 to your computer and use it in GitHub Desktop.

Select an option

Save anderssvendal/3052679 to your computer and use it in GitHub Desktop.

Revisions

  1. anderssvendal revised this gist Jul 5, 2012. 1 changed file with 13 additions and 0 deletions.
    13 changes: 13 additions & 0 deletions keyboard.m
    Original file line number Diff line number Diff line change
    @@ -20,4 +20,17 @@ - (void)adjustViewForTextField:(UITextField*)textField animated:(BOOL)animated {
    self.view.frame = frame;
    }
    }
    }

    - (void)resetViewAfterEditingAnimated:(BOOL)animated {
    CGRect frame = self.view.frame;
    frame.origin.y = 0.0;
    if (animated) {
    [UIView animateWithDuration:0.25 animations:^ {
    self.view.frame = frame;
    }];
    }
    else {
    self.view.frame = frame;
    }
    }
  2. anderssvendal created this gist Jul 5, 2012.
    23 changes: 23 additions & 0 deletions keyboard.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    - (void)adjustViewForTextField:(UITextField*)textField animated:(BOOL)animated {
    CGRect frame = self.view.frame;

    double screenSize = 480.0;
    double topHeight = 20.0;
    double keyboardSize = 216.0;
    double keyboardPos = screenSize - keyboardSize;
    double textFieldPos = topHeight + textField.frame.origin.y;
    double textFieldHeight = textField.frame.size.height;
    double textFieldBottom = textFieldPos + textFieldHeight;

    if ((textFieldBottom + frame.origin.y) >= keyboardPos) {
    frame.origin.y = (keyboardPos - textFieldPos) - textFieldHeight - 10.0;
    if (animated) {
    [UIView animateWithDuration:0.25 animations:^ {
    self.view.frame = frame;
    }];
    }
    else {
    self.view.frame = frame;
    }
    }
    }