diff --git a/Example/KeyboardConstraintDemo/Main.storyboard b/Example/KeyboardConstraintDemo/Main.storyboard index 7be163a..31fa704 100644 --- a/Example/KeyboardConstraintDemo/Main.storyboard +++ b/Example/KeyboardConstraintDemo/Main.storyboard @@ -1,8 +1,13 @@ - - + + + + + - + + + @@ -14,11 +19,11 @@ - + - + @@ -26,25 +31,31 @@ + + + + + - + - + @@ -54,9 +65,4 @@ - - - - - diff --git a/KeyboardConstraint.podspec b/KeyboardConstraint.podspec index 7529be1..452a3ad 100644 --- a/KeyboardConstraint.podspec +++ b/KeyboardConstraint.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'KeyboardConstraint' - s.version = '0.0.2' + s.version = '0.0.3' s.license = 'MIT' s.summary = 'A NSLayoutConstraint subclass which observes keyboard notifications.' s.homepage = 'https://github.com/Papercloud/KeyboardConstraint/' diff --git a/KeyboardConstraint/KeyboardAdjustConstraint.m b/KeyboardConstraint/KeyboardAdjustConstraint.m index e0162ad..d33c394 100644 --- a/KeyboardConstraint/KeyboardAdjustConstraint.m +++ b/KeyboardConstraint/KeyboardAdjustConstraint.m @@ -49,9 +49,27 @@ - (void)_keyboardDidChangeVisible:(NSNotification *)notification endFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; } - UIView *superview = [(UIView *)self.firstItem superview]; + UIView *superview = [(UIView *)self.secondItem superview]; CGRect windowRelativeFrame = [superview convertRect:superview.bounds toView:nil]; - CGFloat keyboardHeight = CGRectGetMaxY(windowRelativeFrame) - CGRectGetMinY(endFrame); + + CGRect intersection = CGRectIntersection(windowRelativeFrame, endFrame); + CGFloat keyboardHeight = 0.0; + + if (!CGRectIsNull(intersection)) { + keyboardHeight = intersection.size.height; + } + + // If one of the constraint items is a layout guide; subtract it's length: + + if (notification.name == UIKeyboardWillShowNotification) { + if ([self.firstItem conformsToProtocol:@protocol(UILayoutSupport)]) { + id layoutGuide = self.firstItem; + keyboardHeight -= layoutGuide.length; + } else if ([self.secondItem conformsToProtocol:@protocol(UILayoutSupport)]) { + id layoutGuide = self.secondItem; + keyboardHeight -= layoutGuide.length; + } + } if (superview) { //Force layout before animation...