Skip to content

Instantly share code, notes, and snippets.

@brandonscript
Last active August 29, 2015 14:06
Show Gist options
  • Save brandonscript/6ec7b778f2c80cab608d to your computer and use it in GitHub Desktop.
Save brandonscript/6ec7b778f2c80cab608d to your computer and use it in GitHub Desktop.
AutoLayout UIScrollView adjustments with arbitrary keyboard sizes on iOS 8
// If you're using Interface Builder, set your UITextView or UIScrollView bottom constraint to the bottom layout guide, constant=0, then create an IBOutlet for the constraint.
// If you're writing it in code, create that constraint programatically and make sure it's a @property.
- (void)viewDidAppear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
// You're probably going to do this, but it's not actually part of the example
_textView.delegate = self;
[_textView becomeFirstResponder];
}
- (void)keyboardDidShow:(NSNotification *)notification
{
[self scrollControlBarTo:notification up:YES];
}
-(void)keyboardDidHide:(NSNotification *)notification
{
[self scrollControlBarTo:notification up:NO];
}
- (void)scrollControlBarTo:(NSNotification *)notification up:(BOOL)up
{
[_keyboardControlsBar layoutIfNeeded];
CGRect keyboardBounds;
[[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardBounds];
[UIView animateWithDuration:0.15 // you might want to play with the timing here, or get really fancy and retrieve it from userInfo.
animations:^{
// This is the @property you created in the comments up top.
_keyboardControlsBarBottomConstraint.constant = (up) ? keyboardBounds.size.height : 0;
[self.view layoutIfNeeded];
}];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment