Skip to content

Commit

Permalink
Merge pull request #9 from Papercloud/issue/8
Browse files Browse the repository at this point in the history
iPhone X Compatibility
  • Loading branch information
markst authored Jan 18, 2018
2 parents 701e546 + 6added4 commit 8a0bd22
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 18 deletions.
36 changes: 21 additions & 15 deletions Example/KeyboardConstraintDemo/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="6250" systemVersion="14A389" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" initialViewController="BYZ-38-t0r">
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13196" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6244"/>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13173"/>
<capability name="Constraints to layout margins" minToolsVersion="6.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--View Controller-->
Expand All @@ -14,37 +19,43 @@
<viewControllerLayoutGuide type="bottom" id="wfy-db-euE"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
<rect key="frame" x="0.0" y="0.0" width="320" height="568"/>
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" borderStyle="roundedRect" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="AMY-Hu-kE3">
<rect key="frame" x="16" y="28" width="288" height="40"/>
<rect key="frame" x="16" y="28" width="343" height="40"/>
<constraints>
<constraint firstAttribute="height" constant="40" id="f9v-zl-Ayg"/>
</constraints>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits"/>
</textField>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="EDp-Dw-qxj">
<rect key="frame" x="16" y="518" width="288" height="30"/>
<rect key="frame" x="16" y="617" width="343" height="50"/>
<color key="backgroundColor" red="0.1764705882" green="0.1764705882" blue="0.2156862745" alpha="1" colorSpace="calibratedRGB"/>
<constraints>
<constraint firstAttribute="height" constant="30" id="hRM-Ec-YVx"/>
<constraint firstAttribute="height" constant="50" id="hRM-Ec-YVx"/>
</constraints>
<state key="normal" title="Dismiss">
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
<color key="titleColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
</state>
<connections>
<action selector="resign:" destination="dkx-z0-nzr" eventType="touchUpInside" id="gmh-iP-Dx7"/>
</connections>
</button>
<view contentMode="scaleToFill" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="kkz-1t-g9C">
<rect key="frame" x="135" y="186" width="240" height="128"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
</view>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstItem="AMY-Hu-kE3" firstAttribute="top" secondItem="y3c-jy-aDJ" secondAttribute="bottom" constant="8" id="3lF-kK-yy4"/>
<constraint firstItem="EDp-Dw-qxj" firstAttribute="trailing" secondItem="8bC-Xf-vdC" secondAttribute="trailingMargin" id="AkY-Vd-oca"/>
<constraint firstItem="AMY-Hu-kE3" firstAttribute="trailing" secondItem="8bC-Xf-vdC" secondAttribute="trailingMargin" id="FWN-Gj-tlx"/>
<constraint firstItem="EDp-Dw-qxj" firstAttribute="leading" secondItem="8bC-Xf-vdC" secondAttribute="leadingMargin" id="TXL-Fw-b7p"/>
<constraint firstItem="wfy-db-euE" firstAttribute="top" secondItem="EDp-Dw-qxj" secondAttribute="bottom" constant="20" id="qny-Gp-4Vs" customClass="KeyboardAdjustConstraint"/>
<constraint firstItem="wfy-db-euE" firstAttribute="top" secondItem="EDp-Dw-qxj" secondAttribute="bottom" id="qny-Gp-4Vs" customClass="KeyboardAdjustConstraint"/>
<constraint firstItem="AMY-Hu-kE3" firstAttribute="leading" secondItem="8bC-Xf-vdC" secondAttribute="leadingMargin" id="z31-z7-nMu"/>
</constraints>
</view>
Expand All @@ -54,9 +65,4 @@
<point key="canvasLocation" x="340" y="579"/>
</scene>
</scenes>
<simulatedMetricsContainer key="defaultSimulatedMetrics">
<simulatedStatusBarMetrics key="statusBar"/>
<simulatedOrientationMetrics key="orientation"/>
<simulatedScreenMetrics key="destination" type="retina4"/>
</simulatedMetricsContainer>
</document>
2 changes: 1 addition & 1 deletion KeyboardConstraint.podspec
Original file line number Diff line number Diff line change
@@ -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/'
Expand Down
22 changes: 20 additions & 2 deletions KeyboardConstraint/KeyboardAdjustConstraint.m
Original file line number Diff line number Diff line change
Expand Up @@ -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<UILayoutSupport> layoutGuide = self.firstItem;
keyboardHeight -= layoutGuide.length;
} else if ([self.secondItem conformsToProtocol:@protocol(UILayoutSupport)]) {
id<UILayoutSupport> layoutGuide = self.secondItem;
keyboardHeight -= layoutGuide.length;
}
}

if (superview) {
//Force layout before animation...
Expand Down

0 comments on commit 8a0bd22

Please sign in to comment.