25 Commits

Author SHA1 Message Date
Drew Olbrich 368383e16b Add InsetContentViewKeyboardTests 2019-03-22 11:00:01 -07:00
Drew Olbrich a42b0346f9 Reorder properties 2019-03-22 07:00:08 -07:00
Drew Olbrich 27ded10c91 Add additional safe area insets for a navigation bar and tab bar in the keyboard tests 2019-03-22 06:59:48 -07:00
Drew Olbrich 534580ee41 Update comments 2019-03-22 06:52:03 -07:00
Drew Olbrich d453927276 Update comments 2019-03-21 21:20:40 -07:00
Drew Olbrich 9358ba3e5c Add testPresentedKeyboardWithFixedContentView 2019-03-21 21:02:52 -07:00
Drew Olbrich 8e6c9b5a23 Fix bug: Content view isn't sized correctly when it doesn't fill the screen
This commit addresses an error in the earlier commit 04b4f8e, which did
not the case when shouldResizeContentViewForKeyboard was false.
2019-03-21 20:46:42 -07:00
Drew Olbrich 56ed1aff1d Make contentView a link 2019-03-19 21:44:05 -07:00
Drew Olbrich 13a0f3ffe3 Indicate that subviews may also be made scrollable 2019-03-19 21:41:07 -07:00
Drew Olbrich 2ece9b58bc Set version to 1.2.0 2019-03-19 21:21:19 -07:00
Drew Olbrich 04b4f8eed5 Fix bug: Content view isn't sized correctly when it doesn't fill the screen
When a content view doesn't fill the screen and the keyboard is presented, the
content view wasn't sized correctly.
2019-03-19 21:18:28 -07:00
Drew Olbrich 0f21ade764 Fix typo 2019-03-18 20:25:57 -07:00
Drew Olbrich 3b99264884 Add scrollRectToVisible link 2019-02-16 11:49:55 -08:00
Drew Olbrich 654b4949b2 Add intrinsicContentSize link 2019-02-16 11:48:08 -08:00
Drew Olbrich 0a4796a643 Add required priority link 2019-02-16 11:46:43 -08:00
Drew Olbrich c3062e345a Adjust contrast 2019-02-15 09:01:54 -08:00
Drew Olbrich ba11e04deb Update background 2019-02-12 07:35:17 -08:00
Drew Olbrich 8c97f5d83a Update usage 2019-02-10 11:27:49 -08:00
Drew Olbrich 4f85c08a54 Update Special Cases Handled 2019-02-10 08:45:25 -08:00
Drew Olbrich ec97d4b936 Update Additional Safe Area Insets 2019-02-10 08:41:53 -08:00
Drew Olbrich 846852a67f Fix typo 2019-02-10 08:38:34 -08:00
Drew Olbrich 809c0dabc5 Update background 2019-02-10 08:34:25 -08:00
Drew Olbrich 1febd4694b Update description 2019-02-10 08:04:09 -08:00
Drew Olbrich 551d94f52c Update Usage Without Subclassing example code 2019-02-09 18:58:47 -08:00
Drew Olbrich 65c738f3e4 Fix typo 2019-02-09 18:31:50 -08:00
15 changed files with 322 additions and 115 deletions
@@ -163,7 +163,7 @@ class SignUpViewController: ScrollingContentViewController {
logoImageView.setContentHuggingPriority(.required, for: .vertical)
logoImageView.setContentHuggingPriority(.required, for: .horizontal)
contentView.addConstraints(constraints)
NSLayoutConstraint.activate(constraints)
}
private func addPillViewConstraints(to pillView: UIView) {
Binary file not shown.

Before

Width:  |  Height:  |  Size: 111 KiB

After

Width:  |  Height:  |  Size: 139 KiB

+20 -16
View File
@@ -21,19 +21,21 @@
## Overview
ScrollingContentViewController makes it easy to create a view controller with a scrolling content view, or to convert an existing static view controller into one that scrolls. Most importantly, it takes care of several tricky undocumented edge cases involving the keyboard, navigation controllers, and device rotations.
ScrollingContentViewController makes it easy to create a view controller with a single scrolling content view, or to convert an existing static view controller into one that scrolls. Most importantly, it takes care of several tricky undocumented edge cases involving the keyboard, navigation controllers, and device rotations.
## Background
A common UIKit Auto Layout task involves creating a view controller with a fixed layout that is too large to fit older, smaller devices, or devices in landscape orientation, or the area of the screen that remains visible when the keyboard is presented.
A common UIKit Auto Layout task involves creating a view controller with a fixed layout that is too large to fit older, smaller devices, or devices in landscape orientation, or the area of the screen that remains visible when the keyboard is presented. The problem is compounded when [Dynamic Type](https://developer.apple.com/documentation/uikit/uifont/scaling_fonts_automatically) is used to support large font sizes.
For example, consider this sign up screen, which fits iPhone Xs, but not iPhone SE with a keyboard:
<img src="https://github.com/drewolbrich/ScrollingContentViewController/raw/master/Images/Overview-Comparison.png" width="888px">
This case can be handled by nesting the view inside a scroll view. You can do this manually in Interface Builder, as described by Apple's [Working with Scroll Views](https://developer.apple.com/library/archive/documentation/UserExperience/Conceptual/AutolayoutPG/WorkingwithScrollViews.html) documentation, but many steps are required. If your view contains text fields, you'll have to write code to compensate for the keyboard when it's presented, as in [Managing the Keyboard](https://developer.apple.com/library/archive/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html#//apple_ref/doc/uid/TP40009542-CH5-SW3). However, handling the keyboard robustly is [surprisingly complicated](#keyboard-resize-filtering), especially if your app presents a sequence of screens with keyboards in the context of a navigation controller, or when device orientation support is required.
This case can be handled by nesting the view inside a scroll view. You could do this manually in Interface Builder, as described by Apple's [Working with Scroll Views](https://developer.apple.com/library/archive/documentation/UserExperience/Conceptual/AutolayoutPG/WorkingwithScrollViews.html) documentation, but many steps are required. If your view contains text fields, you'll have to write code to adjust the view to compensate for the presented keyboard, as described in [Managing the Keyboard](https://developer.apple.com/library/archive/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html#//apple_ref/doc/uid/TP40009542-CH5-SW3). However, handling the keyboard robustly is [surprisingly complicated](#keyboard-resize-filtering), especially if your app presents a sequence of screens with keyboards in the context of a navigation controller, or when device orientation support is required.
To simplify this task, ScrollingContentViewController inserts the scroll view into the view hierarchy for you, along with all necessary Auto Layout constraints. When used in a storyboard, ScrollingContentViewController exposes a [`contentView`](#contentView) outlet that you connect to the view that you'd like make scrollable. Everything else is taken care of automatically.
To simplify this task, ScrollingContentViewController inserts the scroll view into the view hierarchy for you at run time, along with all necessary Auto Layout constraints.
When used in a storyboard, ScrollingContentViewController exposes an outlet called [`contentView`](#contentView) that you connect to the view that you'd like to make scrollable. This may be the view controller's root view. Everything else is taken care of automatically, including responding to keyboard presentation and device orientation changes.
ScrollingContentViewController can be configured using storyboards or entirely in code. The easiest way to use it is by subclassing the `ScrollingContentViewController` class instead of [`UIViewController`](https://developer.apple.com/documentation/uikit/uiviewcontroller). However, when this is not an option, a helper class called `ScrollingContentViewManager` can be composed with your existing view controller class instead.
@@ -57,7 +59,7 @@ github "drewolbrich/ScrollingContentViewController"
Subclasses of `ScrollingContentViewController` may be configured using [storyboards](#storyboards) or in [code](#code).
This library can also be used without subclassing, by composing the helper class `ScrollingContentViewManager` instead. See [Usage Without Subclassing](#usage-without-subclassing).
This library may also be used without subclassing, by composing the helper class `ScrollingContentViewManager` instead. Refer to [Usage Without Subclassing](#usage-without-subclassing).
### Storyboards
@@ -75,7 +77,7 @@ To configure `ScrollingContentViewController` in a storyboard:
}
```
2. In Interface Builder's outline view, control-click your view controller and connect its [`contentView`](#contentView) outlet to your view controller's root view.
2. In Interface Builder's outline view, control-click your view controller and connect its [`contentView`](#contentView) outlet to your view controller's root view or any other subview that you want to make scrollable.
<img src="https://github.com/drewolbrich/ScrollingContentViewController/raw/master/Images/Usage-Storyboards.png" width="471px">
@@ -89,7 +91,7 @@ To configure `ScrollingContentViewController` in a storyboard:
}
```
4. At runtime, the `ScrollingContentViewController` property [`contentView`](#contentView) will now reference the superview of the controls that you laid out in Interface Builder. This superview will no longer be referenced by the [`view`](https://developer.apple.com/documentation/uikit/uiviewcontroller/1621460-view) property, which will instead reference an empty root view behind the scrolling content view. If necessary, revise your code to reflect this change.
4. At run time, the `ScrollingContentViewController` property [`contentView`](#contentView) will now reference the superview of the controls that you laid out in Interface Builder. This superview will no longer be referenced by the [`view`](https://developer.apple.com/documentation/uikit/uiviewcontroller/1621460-view) property, which will instead reference an empty root view behind the scrolling content view. If necessary, revise your code to reflect this change.
Your content view will now scroll, provided that you ensure that the content view's Auto Layout constraints [sufficiently define its size](#auto-layout-considerations), and that this size is larger than the safe area.
@@ -121,6 +123,8 @@ To integrate `ScrollingContentViewController` programmatically:
// ...
}
```
You may also assign [`contentView`](#contentView) to a subview of your view controller's root view, in which case only that subview will be made scrollable.
## Caveats
@@ -134,7 +138,7 @@ If you'd like your content view to stretch to take advantage of the full visible
To determine the size of the scroll view's content size, ScrollingContentViewController creates width and height constraints with a relation greater than or equal to the width and height of the scroll view's safe area. The priority of these constraints is 500. Consequently, if you create an unbroken chain of constraints with priority [`defaultHigh`](https://developer.apple.com/documentation/uikit/uilayoutpriority/1622249-defaulthigh) (750) or [`required`](https://developer.apple.com/documentation/uikit/uilayoutpriority/1622241-required) (1000), they will take precedence over ScrollingContentViewController's internal minimum width and height constraints, and your content view will not stretch to fill the scroll view's safe area.
If the size of your view controller is intentionally highly constrained (e.g. consisting exclusively of constraints with `required` priority and lacking [`greaterThanOrEqual`](https://developer.apple.com/documentation/uikit/nslayoutconstraint/relation/greaterthanorequal) relation constraints), you may see Auto Layout constraint errors in Interface Builder if the constraints don't match the simulated size of the view, for example, when you switch between simulated device sizes. The easiest way to resolve this issue is to reduce the priority of one of your constraints. The value 240 is a good choice because it is lower than the default content hugging priority (250) and consequently, it will help avoid the undesirable behavior where text fields and labels without height constraints stretch vertically.
If the size of your view controller is intentionally highly constrained (e.g. consisting exclusively of constraints with [`required`](https://developer.apple.com/documentation/uikit/uilayoutpriority/1622241-required) priority and lacking [`greaterThanOrEqual`](https://developer.apple.com/documentation/uikit/nslayoutconstraint/relation/greaterthanorequal) relation constraints), you may see Auto Layout constraint errors in Interface Builder if the constraints don't match the simulated size of the view, for example, when you switch between simulated device sizes. The easiest way to resolve this issue is to reduce the priority of one of your constraints. The value 240 is a good choice because it is lower than the default content hugging priority (250) and consequently, it will help avoid the undesirable behavior where text fields and labels without height constraints stretch vertically.
<img src="https://github.com/drewolbrich/ScrollingContentViewController/raw/master/Images/Usage-Auto-Layout-Considerations.png" width="663px">
@@ -142,7 +146,7 @@ If the size of your view controller is intentionally highly constrained (e.g. co
If you'd prefer not to use Auto Layout, the content view's size may be specified using [`intrinsicContentSize`](https://developer.apple.com/documentation/uikit/uiview/1622600-intrinsiccontentsize) instead of constraints.
The default `UIView` content hugging priority is [`defaultLow`](https://developer.apple.com/documentation/uikit/uilayoutpriority/1622250-defaultlow), and consequently, the content view's intrinisic content size will normally be overridden by the minimum size constraints that ScrollingContentViewController assigns. If you'd like `intrinsicContentSize` to take precedence over these constraints, set the content view's content hugging priority to [`required`](https://developer.apple.com/documentation/uikit/uilayoutpriority/1622241-required).
The default `UIView` content hugging priority is [`defaultLow`](https://developer.apple.com/documentation/uikit/uilayoutpriority/1622250-defaultlow), and consequently, the content view's intrinisic content size will normally be overridden by the minimum size constraints that ScrollingContentViewController assigns. If you'd like [`intrinsicContentSize`](https://developer.apple.com/documentation/uikit/uiview/1622600-intrinsiccontentsize) to take precedence over these constraints, set the content view's content hugging priority to [`required`](https://developer.apple.com/documentation/uikit/uilayoutpriority/1622241-required).
### Changing the Background Color
@@ -255,7 +259,7 @@ class MyViewController: UIViewController {
lazy var scrollingContentViewManager = ScrollingContentViewManager(hostViewController: self)
var contentView = UIView()
let contentView = UIView()
override func viewDidLoad() {
super.viewDidLoad()
@@ -387,7 +391,7 @@ When the content view is first assigned, if it has a superview, the scroll view
If the content view has no superview, the scroll view is parented to the view controller's root view and its frame and autoresizing mask are defined to track the root view's bounds.
If the ScrollingContentViewController's `contentView` property references its root view, a new `UIView` is allocated and replaces it as the root view so that the scroll view will have an appropriate view to be parented view.
If the ScrollingContentViewController's `contentView` property references its root view, a new `UIView` is allocated and replaces it as the root view so that the scroll view will have an appropriate view to be parented to.
The content view's superview does not necessarily have to be the view controller's root view, and does not have to match the root view's size.
@@ -399,13 +403,13 @@ When the keyboard is presented, ScrollingContentViewController modifies the cont
Although ScrollingContentViewController modifies [`additionalSafeAreaInsets`](https://developer.apple.com/documentation/uikit/uiviewcontroller/2902284-additionalsafeareainsets) when the keyboard is presented, it restores it to its original value when the keyboard is dismissed. This allows [`additionalSafeAreaInsets`](https://developer.apple.com/documentation/uikit/uiviewcontroller/2902284-additionalsafeareainsets) to be used for other purposes, such as custom tool palettes.
During development, an alternate approach suggested by Apple, modifying the scroll view's content size, was also tried. This requires adjusting the scroll view's [`scrollIndicatorInsets`](https://developer.apple.com/documentation/uikit/uiscrollview/1619427-scrollindicatorinsets) property to compensate for the content size change. On iPhone Xs in landscape orientation, doing so has the unfortunate side effect of awkwardly shifting the scroll indicator away from the edge of the screen.
During development, an alternate approach suggested by Apple, modifying the scroll view's content size, was also tried. This requires adjusting the scroll view's [`scrollIndicatorInsets`](https://developer.apple.com/documentation/uikit/uiscrollview/1619427-scrollindicatorinsets) property to compensate for the content size change. Unfortunately, on iPhone Xs in landscape orientation, doing so has the side effect of awkwardly shifting the scroll indicator away from the edge of the screen.
### Keyboard Resize Filtering
When a text field becomes the first responder, UIKit presents the keyboard. If the user taps another text field, changing the first responder, UIKit may adjust the keyboard's height if an input accessory view is specified. These changes may generate a sequence of [`keyboardWillShow`](https://developer.apple.com/documentation/uikit/uiresponder/1621576-keyboardwillshownotification) notifications, each with different keyboard heights.
As an extreme example, if the user populates a text field by tapping on an AutoFill input accessory view, and this action causes a password text field to automatically become the first responder, one [`keyboardWillHide`](https://developer.apple.com/documentation/uikit/uikeyboardwillhidenotification) notification and two [`keyboardWillShow`](https://developer.apple.com/documentation/uikit/uiresponder/1621576-keyboardwillshownotification) notifications will be posted within a span of 0.1 seconds.
As an extreme example, if the user populates an email text field by tapping on an AutoFill input accessory view item, and this action has the side effect of causing a password text field to become the first responder, one [`keyboardWillHide`](https://developer.apple.com/documentation/uikit/uikeyboardwillhidenotification) notification and two [`keyboardWillShow`](https://developer.apple.com/documentation/uikit/uiresponder/1621576-keyboardwillshownotification) notifications will be posted within a span of 0.1 seconds.
If ScrollingContentViewController were to respond to each of these notifications individually, this would cause awkward discontinuities in the scroll view animation that accompanies changes to the keyboard's height.
@@ -413,7 +417,7 @@ To work around this issue, ScrollingContentViewController filters out sequences
During a device orientation transition, a [`keyboardWillHide`](https://developer.apple.com/documentation/uikit/uikeyboardwillhidenotification) notification is posted before the animation starts, followed by [`keyboardWillShow`](https://developer.apple.com/documentation/uikit/uiresponder/1621576-keyboardwillshownotification) after it ends, even though the keyboard remains visible during the transition. Because the duration of the animation exceeds the filtering time window, it is therefore necessary to temporarily suspend filtering during the transition. Otherwise, the content view would resize unnecessarily.
Finally, ScrollingContentViewController correctly handles the case where changes to the size or layout of the scroll view's content may occur in response to keyboard presentation or device orientation changes (in particular when [`shouldResizeContentViewForKeyboard`](#shouldResizeContentViewForKeyboard) is `true`), invaliding the coordinate space of the rectangle passed to `scrollRectToVisible` (most importantly, in the case when that method is called automatically by iOS after keyboard changes) which would otherwise result in the scroll view scrolling by an inappropriate amount or leaving the scroll view with a content offset that is outside of the legal scrolling range.
Finally, ScrollingContentViewController correctly handles the case where changes to the size or layout of the scroll view's content may occur in response to keyboard presentation or device orientation changes (in particular when [`shouldResizeContentViewForKeyboard`](#shouldResizeContentViewForKeyboard) is `true`), invaliding the coordinate space of the rectangle passed to [`scrollRectToVisible`](https://developer.apple.com/documentation/uikit/uiscrollview/1619439-scrollrecttovisible) (most importantly, in the case when that method is called automatically by iOS after keyboard changes) which would otherwise result in the scroll view scrolling by an inappropriate amount or leaving the scroll view with a content offset that is outside of the legal scrolling range.
Refer to Apple's [Managing the Keyboard](https://developer.apple.com/library/archive/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html#//apple_ref/doc/uid/TP40009542-CH5-SW3) documentation for more information about responding to changes in keyboard visibility.
@@ -429,7 +433,7 @@ ScrollingContentViewController correctly handles sequences of pushed view contro
When device orientation changes occur, ScrollingContentViewController improves upon the default scroll view behavior by pinning the upper left corner of the scroll view in place, while at the same time preventing out of range content offsets. This matches the behavior of many of Apple's iOS apps.
### keyboardDismissMode Enhancement
### keyboardDismissMode
ScrollingContentViewController automatically enables [`UIScrollView.alwaysBounceVertical`](https://developer.apple.com/documentation/uikit/uiscrollview/1619383-alwaysbouncevertical) while the keyboard is presented if [`UIScrollView.keyboardDismissMode`](https://developer.apple.com/documentation/uikit/uiscrollview/1619437-keyboarddismissmode) is set to anything other than [`none`](https://developer.apple.com/documentation/uikit/uiscrollview/keyboarddismissmode/none), so the keyboard can be dismissed even if the view is too short to normally allow scrolling.
@@ -437,7 +441,7 @@ ScrollingContentViewController automatically enables [`UIScrollView.alwaysBounce
ScrollingContentViewController correctly handles the case when the scroll view doesn't cover the full extent of the screen, in which case it may only partially intersect the keyboard.
### Text Field Animation Artifact Fix
### Text Field Animation Artifacts
As of iOS 12, if the user taps a sequence of custom text fields, UIKit may awkwardly animate the text field's text. ScrollingContentViewController suppresses this animation.
+5 -4
View File
@@ -1,13 +1,14 @@
Pod::Spec.new do |s|
s.name = 'ScrollingContentViewController'
s.version = '1.1.0'
s.version = '1.2.0'
s.summary = 'A Swift library that simplifies making a view controller\'s view scrollable'
s.description = <<-DESC
ScrollingContentViewController makes it easy to create a view controller with a
scrolling content view, or to convert an existing static view controller into
one that scrolls. Most importantly, it takes care of several tricky undocumented
edge cases involving the keyboard, navigation controllers, and device rotations.
single scrolling content view, or to convert an existing static view controller
into one that scrolls. Most importantly, it takes care of several tricky
undocumented edge cases involving the keyboard, navigation controllers, and
device rotations.
DESC
s.homepage = 'https://github.com/drewolbrich/ScrollingContentViewController'
@@ -34,6 +34,7 @@
3A83273921E700A000E8D95C /* SignUpController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A83273721E700A000E8D95C /* SignUpController.swift */; };
3A83273B21E703F600E8D95C /* SignUpControllerDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A83273A21E703F600E8D95C /* SignUpControllerDelegate.swift */; };
3A83273C21E703F600E8D95C /* SignUpControllerDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A83273A21E703F600E8D95C /* SignUpControllerDelegate.swift */; };
3A881DB9224525F500E21CA2 /* InsetContentViewKeyboardTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A881DB72245257E00E21CA2 /* InsetContentViewKeyboardTests.swift */; };
3AAC048F21E2D4C500D94DA5 /* ScrollingContentViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AAC048E21E2D4C500D94DA5 /* ScrollingContentViewController.swift */; };
3AAC049121E2D4F100D94DA5 /* ScrollingContentViewManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AAC049021E2D4F100D94DA5 /* ScrollingContentViewManager.swift */; };
3AAC049821E2F01C00D94DA5 /* UIResponder+Current.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AAC049321E2F01C00D94DA5 /* UIResponder+Current.swift */; };
@@ -215,6 +216,7 @@
3A7014BB21EBD723002C6740 /* LaunchScreen.storyboard */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; path = LaunchScreen.storyboard; sourceTree = "<group>"; };
3A83273721E700A000E8D95C /* SignUpController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SignUpController.swift; sourceTree = "<group>"; };
3A83273A21E703F600E8D95C /* SignUpControllerDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SignUpControllerDelegate.swift; sourceTree = "<group>"; };
3A881DB72245257E00E21CA2 /* InsetContentViewKeyboardTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InsetContentViewKeyboardTests.swift; sourceTree = "<group>"; };
3AAC048821E2D3FD00D94DA5 /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
3AAC048921E2D3FD00D94DA5 /* LICENSE */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = LICENSE; sourceTree = "<group>"; };
3AAC048A21E2D3FD00D94DA5 /* ScrollingContentViewController.podspec */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = ScrollingContentViewController.podspec; sourceTree = "<group>"; };
@@ -395,6 +397,7 @@
3AD597CA21F3995A00F220A0 /* ManagerTests.swift */,
3AD597CE21F3AD8000F220A0 /* IntrinsicSizeTests.swift */,
3AD597D021F3B70400F220A0 /* KeyboardTests.swift */,
3A881DB72245257E00E21CA2 /* InsetContentViewKeyboardTests.swift */,
3A3652F521F38E750010CE55 /* StoryboardTests.storyboard */,
3A3652F721F390110010CE55 /* ContentView.swift */,
3AD597CC21F3AC2000F220A0 /* IntrinsicSizeContentView.swift */,
@@ -815,6 +818,7 @@
3A3652F821F390110010CE55 /* ContentView.swift in Sources */,
3A5702DD21E2CBB600E4CC55 /* StoryboardTests.swift in Sources */,
3AD597CF21F3AD8000F220A0 /* IntrinsicSizeTests.swift in Sources */,
3A881DB9224525F500E21CA2 /* InsetContentViewKeyboardTests.swift in Sources */,
3AD597D121F3B70400F220A0 /* KeyboardTests.swift in Sources */,
3AD597C921F3964000F220A0 /* CodeTests.swift in Sources */,
3AD597CD21F3AC2000F220A0 /* IntrinsicSizeContentView.swift in Sources */,
+18 -17
View File
@@ -12,7 +12,7 @@ import UIKit
/// An object that adjusts the host view controller's
/// `additionalSafeAreaInsets.bottom` property to compensate for the portion of the
/// keyboard that overlaps the scroll view.
/// keyboard that overlaps the host view controller's root view.
internal class AdditionalSafeAreaInsetsController {
private weak var delegate: AdditionalSafeAreaInsetsControlling?
@@ -26,12 +26,12 @@ internal class AdditionalSafeAreaInsetsController {
self.delegate = delegate
}
/// The height of the portion of the keyboard that overlaps the host view
/// controller's root view.
var bottomInset: CGFloat = 0 {
didSet {
guard let delegate = delegate,
let hostViewController = delegate.hostViewController,
let contentViewMinimumHeightConstraint = delegate.contentViewMinimumHeightConstraint else {
return
guard let hostViewController = delegate?.hostViewController else {
return
}
var adjustedBottomInset = bottomInset
@@ -40,6 +40,8 @@ internal class AdditionalSafeAreaInsetsController {
let initialAdditionalSafeAreaInsets = hostViewController.additionalSafeAreaInsets
self.initialAdditionalSafeAreaInsets = initialAdditionalSafeAreaInsets
adjustedBottomInset = max(adjustedBottomInset, initialAdditionalSafeAreaInsets.bottom)
self.delegate?.additionalSafeAreaInsetsControllerWillUpdateAdditionalSafeAreaInsetsForPresentedKeyboard(self)
setAdditionalSafeAreaBottomInset(adjustedBottomInset)
} else if bottomInset == 0 && oldValue != 0 {
// The keyboard was dismissed.
guard let initialAdditionalSafeAreaInsets = initialAdditionalSafeAreaInsets else {
@@ -48,6 +50,8 @@ internal class AdditionalSafeAreaInsetsController {
}
adjustedBottomInset = initialAdditionalSafeAreaInsets.bottom
self.initialAdditionalSafeAreaInsets = nil
setAdditionalSafeAreaBottomInset(adjustedBottomInset)
self.delegate?.additionalSafeAreaInsetsControllerDidUpdateAdditionalSafeAreaInsetsForDismissedKeyboard(self)
} else if bottomInset != oldValue {
// The keyboard changed size.
guard let initialAdditionalSafeAreaInset = initialAdditionalSafeAreaInsets else {
@@ -55,23 +59,20 @@ internal class AdditionalSafeAreaInsetsController {
return
}
adjustedBottomInset = max(adjustedBottomInset, initialAdditionalSafeAreaInset.bottom)
setAdditionalSafeAreaBottomInset(adjustedBottomInset)
} else {
// The size of the keyboard is unchanged.
return
}
if delegate.shouldResizeContentViewForKeyboard {
// Adjust the additional safe area insets, possibly reducing the size
// of the content view.
hostViewController.additionalSafeAreaInsets.bottom = adjustedBottomInset
} else {
// Adjust the additional safe area insets, but also increase the minimum height of
// the content view to compensate. The size of the content view will remain
// unchanged.
hostViewController.additionalSafeAreaInsets.bottom = adjustedBottomInset
contentViewMinimumHeightConstraint.constant = adjustedBottomInset
}
}
}
private func setAdditionalSafeAreaBottomInset(_ additionalSafeAreaBottomInset: CGFloat) {
guard let hostViewController = delegate?.hostViewController else {
return
}
hostViewController.additionalSafeAreaInsets.bottom = additionalSafeAreaBottomInset
}
}
@@ -13,14 +13,15 @@ import UIKit
/// Delegate for `AdditionalSafeAreaInsetsController`.
internal protocol AdditionalSafeAreaInsetsControlling: class {
/// View controller whose `additionalSafeAreaInsets` property is manipulated.
/// The view controller whose `additionalSafeAreaInsets` property is manipulated.
var hostViewController: UIViewController? { get }
/// Manipulated content view minimum height constraint.
var contentViewMinimumHeightConstraint: NSLayoutConstraint? { get }
/// Tells the delegate that the host view controller's additional safe area insets
/// are about to be updated because the keyboard has been presented.
func additionalSafeAreaInsetsControllerWillUpdateAdditionalSafeAreaInsetsForPresentedKeyboard(_ additionalSafeAreaInsetsController: AdditionalSafeAreaInsetsController)
/// If `true`, the content view is allowed to shrink to compensate for the reduced
/// visible area of the screen when the keyboard is presented.
var shouldResizeContentViewForKeyboard: Bool { get }
/// Tells the delegate that the host view controller's additional safe area insets
/// have been restored to their original values after the keyboard was dismissed.
func additionalSafeAreaInsetsControllerDidUpdateAdditionalSafeAreaInsetsForDismissedKeyboard(_ additionalSafeAreaInsetsController: AdditionalSafeAreaInsetsController)
}
+1 -1
View File
@@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.1.0</string>
<string>1.2.0</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
</dict>
+30 -29
View File
@@ -120,7 +120,7 @@ internal class KeyboardObserver: NSObject {
}
}
// Continues in adjustViewForKeyboard(withKeyboardFrameEvent:)...
// Continues in scrollViewFilter(_:adjustViewForKeyboardFrameEvent:)...
}
/// Updates the view controller to compensate for the current state of the keyboard.
@@ -142,11 +142,11 @@ internal class KeyboardObserver: NSObject {
scrollViewFilter.flush()
}
// Continues in adjustViewForKeyboard(withKeyboardFrameEvent:)...
// Continues in scrollViewFilter(_:adjustViewForKeyboardFrameEvent:)...
}
}
/// Tests submitting a keyboard frame event.
/// Tests submitting a keyboard frame event. This method is used by unit tests only.
internal func testKeyboardFrameEvent(_ keyboardFrameEvent: KeyboardFrameEvent) {
// This method is intended for use in unit tests only.
assert(isUnitTest)
@@ -158,7 +158,7 @@ internal class KeyboardObserver: NSObject {
scrollViewFilter.submitKeyboardFrameEvent(keyboardFrameEvent)
scrollViewFilter.flush()
// Continues in adjustViewForKeyboard(withKeyboardFrameEvent:)...
// Continues in scrollViewFilter(_:adjustViewForKeyboardFrameEvent:)...
}
/// Suppresses unwanted text field text animation.
@@ -261,44 +261,45 @@ extension KeyboardObserver: ScrollViewFilterKeyboardDelegate {
assert(isAdjustingViewForKeyboardFrameEvent)
}
/// Returns the height of portion of the keyboard's frame that overlaps the scroll
/// view.
/// Returns the height of portion of the keyboard's frame that overlaps the host
/// view controller's root view.
///
/// This method correctly handles the case where the view doesn't cover the entire
/// screen.
/// This method correctly handles the case where the view controller's root view
/// does not cover the entire screen, for example if the view controller is composed
/// within another view controller.
///
/// - Parameter notification: The keyboard notification that provides the keyboard's
/// frame.
/// - Returns: The height of portion of the keyboard's frame that overlaps the view.
/// - Returns: The height of portion of the keyboard's frame that overlaps the view
/// controller's root view.
private func bottomInset(from keyboardFrame: CGRect?) -> CGFloat? {
guard let keyboardFrame = keyboardFrame,
let hostViewController = delegate?.hostViewController,
let view = hostViewController.view,
let rootView = hostViewController.view,
// UIApplication.shared.keyWindow is nil when unit tests are executing, and
// view.window is nil outside of unit tests in the case when a view is being
// pushed.
let window = isUnitTest ? view.window : UIApplication.shared.keyWindow else {
// rootView is nil outside of unit tests in the case when a view is being pushed.
let window = isUnitTest ? rootView.window : UIApplication.shared.keyWindow else {
return nil
}
// The frame of the view in the window's coordinate space.
let viewFrameInWindow = window.convert(view.frame, from: view.superview)
// The frame of the view controller's root view in the window's coordinate space.
let rootViewFrameInWindow = window.convert(rootView.frame, from: rootView.superview)
// The intersection of the keyboard's frame with the frame of the view in the
// window's coordinate space.
let keyboardViewIntersectionFrameInWindow = viewFrameInWindow.intersection(keyboardFrame)
// The intersection of the keyboard's frame with the frame of the root view in
// the window's coordinate space.
let keyboardViewIntersectionFrameInWindow = rootViewFrameInWindow.intersection(keyboardFrame)
// The intersection of the keyboard's frame with the frame of the view in the
// view's coordinate space.
let keyboardViewIntersectionFrameInView = window.convert(keyboardViewIntersectionFrameInWindow, to: view)
// The intersection of the keyboard's frame with the frame of the root view in
// the root view's coordinate space.
let keyboardViewIntersectionFrameInRootView = window.convert(keyboardViewIntersectionFrameInWindow, to: rootView)
// The height of the region of the keyboard that overlaps the view.
let overlappingKeyboardHeight = keyboardViewIntersectionFrameInView.height
// The height of the region of the keyboard that overlaps the root view.
let overlappingKeyboardHeight = keyboardViewIntersectionFrameInRootView.height
// The view's safe area bottom inset.
let safeAreaBottomInset = hostViewController.view.safeAreaInsets.bottom
// The root view safe area bottom inset.
let safeAreaBottomInset = rootView.safeAreaInsets.bottom
// The view's additional safe area bottom inset.
// The host view controller's additional safe area bottom inset.
let additionalSafeAreaBottomInset = hostViewController.additionalSafeAreaInsets.bottom
// The bottom safe area bottom inset, excluding the additional safe area inset.
@@ -308,10 +309,10 @@ extension KeyboardObserver: ScrollViewFilterKeyboardDelegate {
// into account, which would otherwise result in a negative number.
let baseSafeAreaBottomInset = max(0, safeAreaBottomInset - additionalSafeAreaBottomInset)
// The height of area of the keyboard's frame that overlaps the view.
let keyboardHeightOverlappingView = max(0, overlappingKeyboardHeight - baseSafeAreaBottomInset)
// The height of area of the keyboard's frame that overlaps the root view.
let keyboardHeightOverlappingRootView = max(0, overlappingKeyboardHeight - baseSafeAreaBottomInset)
return keyboardHeightOverlappingView
return keyboardHeightOverlappingRootView
}
}
+3 -3
View File
@@ -26,11 +26,11 @@ internal protocol KeyboardObservering: class {
/// the scroll view obscured by the presented keyboard, if possible.
var shouldResizeContentViewForKeyboard: Bool { get }
/// Adjusts the view to compensate for the portion of the keyboard that overlaps the
/// scroll view.
/// Adjusts the view controller to compensate for the portion of the keyboard that
/// overlaps the view controller's root view.
///
/// - Parameter bottomInset: The height of the vertical extent of the keyboard that
/// overlaps the scroll view.
/// overlaps the view controller's root view.
func adjustViewForKeyboard(withBottomInset bottomInset: CGFloat)
}
+4 -4
View File
@@ -80,7 +80,7 @@ public class ScrollingContentScrollView: UIScrollView {
/// - margin: An optional margin around `rect` that should also be made visible.
/// If `nil`, the value of `visibilityScrollMargin` is used.
public func scrollRectToVisible(_ rect: CGRect, animated: Bool, margin: CGFloat? = nil) {
if let descendantView = self.descendentView(of: self, containing: rect, in: self) {
if let descendantView = self.descendantView(of: self, containing: rect, in: self) {
// If the rect matches the bounds of the descendant view, we'll substitute it with
// nil, which will be replaced with the bounds of the descendant view when it is
// processed later. The handles the case where the descendant view is resized
@@ -141,13 +141,13 @@ public class ScrollingContentScrollView: UIScrollView {
/// - rect: The rectangle to search for, defined in the coordinate space of `rectView`.
/// - rectView: The view that defines the coordinate space in which `rect` is defined.
/// - Returns: The descendant view that contains `rect`.
private func descendentView(of view: UIView, containing rect: CGRect, in rectView: UIView) -> UIView? {
private func descendantView(of view: UIView, containing rect: CGRect, in rectView: UIView) -> UIView? {
let frame = rectView.convert(rect, to: view)
for subview in view.subviews {
// Perform a depth first search so the descendant view with the greatest depth that
// contains the rectangle will be found first.
if let descendentView = descendentView(of: subview, containing: rect, in: rectView) {
return descendentView
if let descendantView = descendantView(of: subview, containing: rect, in: rectView) {
return descendantView
}
if subview.frame.contains(frame) {
return subview
+50 -18
View File
@@ -82,16 +82,19 @@ public class ScrollingContentViewManager: KeyboardObservering, ScrollViewBounceC
/// The default value is `true`.
public var shouldAdjustAdditionalSafeAreaInsetsForKeyboard = true
/// A constraint that enforces a minimum width for the content view.
///
/// The priority of this constraint is `defaultHigh`.
internal private(set) var contentViewMinimumWidthConstraint: NSLayoutConstraint?
/// A constraint that enforces a minimum width for the content view equal to the
/// scroll view's safe area width.
private var contentViewMinimumWidthConstraint: NSLayoutConstraint?
/// A constraint that enforces a minimum height for the content view.
///
/// The priority of this constraint is `defaultHigh`. This constraint's constant is
/// modified by `AdditionalSafeAreaInsetsController`.
internal private(set) var contentViewMinimumHeightConstraint: NSLayoutConstraint?
/// A constraint that enforces a minimum height for the content view equal to the
/// scroll view's safe area height.
private var contentViewMinimumHeightConstraint: NSLayoutConstraint?
/// When the keyboard is presented, if `shouldResizeContentViewForKeyboard` is
/// false, this constraint is assigned to the current height of the content view. It
/// is deactivated when the keyboard is dismissed. This prevents the content view
/// from shrinking in response to the presented keyboard.
private var contentViewMinimumHeightForPresentedKeyboardConstraint: NSLayoutConstraint?
/// An object that responds to notifications posted by UIKit when the keyboard is
/// presented or dismissed, and which adjusts the scroll view to compensate.
@@ -462,8 +465,12 @@ public class ScrollingContentViewManager: KeyboardObservering, ScrollViewBounceC
let contentViewMinimumHeightConstraint = contentView.heightAnchor.constraint(greaterThanOrEqualTo: scrollView.safeAreaLayoutGuide.heightAnchor, multiplier: 1)
self.contentViewMinimumHeightConstraint = contentViewMinimumHeightConstraint
let contentViewMinimumHeightForPresentedKeyboardConstraint = contentView.heightAnchor.constraint(equalToConstant: 0)
self.contentViewMinimumHeightForPresentedKeyboardConstraint = contentViewMinimumHeightForPresentedKeyboardConstraint
contentViewMinimumWidthConstraint.priority = minimumSizeConstraintPriority
contentViewMinimumHeightConstraint.priority = minimumSizeConstraintPriority
contentViewMinimumHeightForPresentedKeyboardConstraint.priority = minimumSizeConstraintPriority
contentView.translatesAutoresizingMaskIntoConstraints = false
@@ -473,10 +480,15 @@ public class ScrollingContentViewManager: KeyboardObservering, ScrollViewBounceC
scrollView.contentLayoutGuide.topAnchor.constraint(equalTo: contentView.topAnchor),
scrollView.contentLayoutGuide.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
contentViewMinimumWidthConstraint,
contentViewMinimumHeightConstraint
contentViewMinimumHeightConstraint,
contentViewMinimumHeightForPresentedKeyboardConstraint
]
scrollView.addConstraints(constraints)
NSLayoutConstraint.activate(constraints)
// This constraint is activated only when the keyboard is presented
// and shouldResizeContentViewForKeyboard is false.
contentViewMinimumHeightForPresentedKeyboardConstraint.isActive = false
}
/// Constrains a scroll view content offset so that it lies within the legal range
@@ -516,20 +528,21 @@ public class ScrollingContentViewManager: KeyboardObservering, ScrollViewBounceC
return scrollView.bounds.inset(by: scrollView.adjustedContentInset).size
}
/// Adjusts the view to compensate for the portion of the keyboard that overlaps the
/// scroll view.
/// Adjusts the view controller to compensate for the portion of the keyboard that
/// overlaps the view controller's root view.
///
/// This method is called by `KeyboardObserver` when the keyboard is presented,
/// dismissed, or changes size.
///
/// - Parameter bottomInset: The height of the area of keyboard's frame that
/// overlaps the view.
/// overlaps the view controller's root view.
func adjustViewForKeyboard(withBottomInset bottomInset: CGFloat) {
self.bottomInset = bottomInset
}
/// The bottom inset to assign to the view controller's additional safe area
/// to compensate for the area of the keyboard that overlaps the scroll view.
/// The bottom inset to assign to the view controller's additional safe area to
/// compensate for the area of the keyboard that overlaps the view controller's root
/// view.
private var bottomInset: CGFloat = 0 {
didSet {
if bottomInset == oldValue {
@@ -540,9 +553,9 @@ public class ScrollingContentViewManager: KeyboardObservering, ScrollViewBounceC
if shouldAdjustAdditionalSafeAreaInsetsForKeyboard {
// When the keyboard is presented, the view controller's
// additionalSafeAreaInsets.bottom property us adjusted to compensate.
// additionalSafeAreaInsets.bottom property is adjusted to compensate.
//
// This approach is chosen instead of resizing the scroll view's content size,
// This approach was chosen instead of resizing the scroll view's content size,
// because doing so requires adjusting its scrollIndicatorInsets property to
// compensate, and on iPhone Xs in landscape orientation, this has the unfortunate
// side effect of awkwardly shifting the scroll indicator away from the edge of the
@@ -555,4 +568,23 @@ public class ScrollingContentViewManager: KeyboardObservering, ScrollViewBounceC
}
}
func additionalSafeAreaInsetsControllerWillUpdateAdditionalSafeAreaInsetsForPresentedKeyboard(_ additionalSafeAreaInsetsController: AdditionalSafeAreaInsetsController) {
guard !shouldResizeContentViewForKeyboard else {
// Don't constrain the height of the keyboard.
return
}
guard let contentViewMinimumHeightForPresentedKeyboardConstraint = contentViewMinimumHeightForPresentedKeyboardConstraint, let contentView = contentView else {
return
}
// When the keyboard is presented, just before AdditionalSafeAreaInsetsController
contentViewMinimumHeightForPresentedKeyboardConstraint.constant = contentView.frame.height
contentViewMinimumHeightForPresentedKeyboardConstraint.isActive = true
}
func additionalSafeAreaInsetsControllerDidUpdateAdditionalSafeAreaInsetsForDismissedKeyboard(_ additionalSafeAreaInsetsController: AdditionalSafeAreaInsetsController) {
contentViewMinimumHeightForPresentedKeyboardConstraint?.isActive = false
}
}
+1 -1
View File
@@ -43,7 +43,7 @@ class ContentView: UIView {
widthConstraint.priority = .defaultLow
heightConstraint.priority = .defaultLow
addConstraints([widthConstraint, heightConstraint])
NSLayoutConstraint.activate([widthConstraint, heightConstraint])
}
}
+138
View File
@@ -0,0 +1,138 @@
//
// InsetContentViewKeyboardTests.swift
// ScrollingContentViewController
//
// Created by Drew Olbrich on 3/22/19.
// Copyright 2019 Oath Inc.
//
// Licensed under the terms of the MIT License. See the file LICENSE for the full terms.
//
import XCTest
@testable import ScrollingContentViewController
/// Test case of presenting the keyboard over a content view that is inset
/// within the host view controller's root view.
class InsetContentViewKeyboardTests: XCTestCase {
var window: UIWindow!
var scrollingContentViewManager: ScrollingContentViewManager!
var hostViewController: UIViewController!
var contentView: UIView!
var scrollView: UIScrollView!
var rootView: UIView!
let navigationBarHeight: CGFloat = 64
let tabBarHeight: CGFloat = 49
let keyboardHeight: CGFloat = 258
let contentViewInset = UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20)
override func setUp() {
window = UIWindow(frame: UIScreen.main.bounds)
window.isHidden = false
hostViewController = UIViewController()
hostViewController.additionalSafeAreaInsets.top = navigationBarHeight
hostViewController.additionalSafeAreaInsets.bottom = tabBarHeight
scrollingContentViewManager = ScrollingContentViewManager(hostViewController: hostViewController)
contentView = UIView()
hostViewController.view.addSubview(contentView)
// Inset the content view within the initial safe area defined by the navigation
// bar and tab bar, but not so much that it won't be overlapped by the keyboard
// when it is presented.
contentView.frame = hostViewController.view.bounds.inset(by: contentViewInset)
contentView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
scrollingContentViewManager.contentView = contentView
hostViewController.beginAppearanceTransition(true, animated: false)
window.rootViewController = hostViewController
hostViewController.view.layoutIfNeeded()
hostViewController.endAppearanceTransition()
scrollView = scrollingContentViewManager.scrollView
rootView = hostViewController.view
}
override func tearDown() {
hostViewController.beginAppearanceTransition(false, animated: false)
window.rootViewController = nil
hostViewController.endAppearanceTransition()
window.isHidden = true
window = nil
hostViewController = nil
scrollView = nil
rootView = nil
contentView = nil
}
/// Tests that the view hierarchy has the expected topology.
func testViewHierarchy() {
// The content view's superview should be the scroll view.
XCTAssertEqual(contentView.superview, scrollView)
// The scroll view's superview should be the view controller's root view.
XCTAssertEqual(scrollView.superview, rootView)
}
/// Tests that the content view and the scroll view have the expected size.
func testDefaultLayout() {
let scrollViewSafeAreaFrame = scrollView.safeAreaLayoutGuide.layoutFrame
// The content view's frame should match the size of the root view's safe area that
// intersects the scroll view frame.
XCTAssertEqual(contentView.frame.size, scrollViewSafeAreaFrame.size)
// The scroll view's content size should match that of the root view's safe area
// that intersects the scroll view frame.
XCTAssertEqual(scrollView.contentSize, scrollViewSafeAreaFrame.size)
}
/// Tests that presenting the keyboard does not affect the size of the content
/// view when `shouldResizeContentViewForKeyboard` is `false`.
func testPresentedKeyboardWithFixedContentView() {
scrollingContentViewManager.shouldResizeContentViewForKeyboard = false
let initialContentViewSize = scrollView.safeAreaLayoutGuide.layoutFrame.size
presentKeyboard()
let expectedContentViewSize = initialContentViewSize
XCTAssertEqual(contentView.frame.size, expectedContentViewSize)
}
/// Tests that presenting the keyboard affects the size of the content view
/// when `shouldResizeContentViewForKeyboard` is `true`.
func testPresentedKeyboardWithResizedContentView() {
scrollingContentViewManager.shouldResizeContentViewForKeyboard = true
let initialContentViewSize = scrollView.safeAreaLayoutGuide.layoutFrame.size
presentKeyboard()
let expectedContentViewSize = CGSize(width: initialContentViewSize.width, height: initialContentViewSize.height - (keyboardHeight - tabBarHeight))
XCTAssertEqual(contentView.frame.size, expectedContentViewSize)
}
private func presentKeyboard() {
let keyboardFrame = CGRect(x: 0, y: window.bounds.height - keyboardHeight, width: window.bounds.width, height: keyboardHeight)
// A test keyboard frame must be injected here because keyboard notifications will
// not be generated when a first responder is assigned within a test.
let keyboardFrameEvent = KeyboardFrameEvent(keyboardFrame: keyboardFrame, duration: 0.35)
scrollingContentViewManager.keyboardObserver?.testKeyboardFrameEvent(keyboardFrameEvent)
}
}
+40 -15
View File
@@ -19,24 +19,29 @@ class KeyboardTests: XCTestCase {
var scrollingContentViewManager: ScrollingContentViewManager!
var hostViewController: UIViewController!
var contentView: ContentView!
var contentView: UIView!
var scrollView: UIScrollView!
var rootView: UIView!
let navigationBarHeight: CGFloat = 64
let tabBarHeight: CGFloat = 49
let keyboardHeight: CGFloat = 258
override func setUp() {
window = UIWindow(frame: UIScreen.main.bounds)
window.isHidden = false
hostViewController = UIViewController()
hostViewController.additionalSafeAreaInsets.top = navigationBarHeight
hostViewController.additionalSafeAreaInsets.bottom = tabBarHeight
scrollingContentViewManager = ScrollingContentViewManager(hostViewController: hostViewController)
contentView = ContentView()
contentView = UIView()
scrollingContentViewManager.contentView = contentView
scrollingContentViewManager.shouldResizeContentViewForKeyboard = true
hostViewController.beginAppearanceTransition(true, animated: false)
window.rootViewController = hostViewController
hostViewController.view.layoutIfNeeded()
@@ -80,23 +85,43 @@ class KeyboardTests: XCTestCase {
XCTAssertEqual(scrollView.contentSize, rootViewSafeAreaSize)
}
/// Tests that presenting the keyboard affects the size of the content view.
func testPresentedKeyboard() {
let keyboardHeight: CGFloat = 258
/// Tests that presenting the keyboard does not affect the size of the content
/// view when `shouldResizeContentViewForKeyboard` is `false`.
func testPresentedKeyboardWithFixedContentView() {
scrollingContentViewManager.shouldResizeContentViewForKeyboard = false
let initialContentViewSize = rootView.bounds.inset(by: rootView.safeAreaInsets).size
presentKeyboard()
let expectedContentViewSize = CGSize(width: initialContentViewSize.width, height: initialContentViewSize.height)
XCTAssertEqual(contentView.frame.size, expectedContentViewSize)
}
/// Tests that presenting the keyboard affects the size of the content view
/// when `shouldResizeContentViewForKeyboard` is `true`.
func testPresentedKeyboardWithResizedContentView() {
scrollingContentViewManager.shouldResizeContentViewForKeyboard = true
let initialContentViewSize = rootView.bounds.inset(by: rootView.safeAreaInsets).size
presentKeyboard()
// The size of the expected safe area of the view controller's root view after the
// keyboard is presented.
let expectedContentViewSize = CGSize(width: initialContentViewSize.width, height: initialContentViewSize.height - (keyboardHeight - tabBarHeight))
XCTAssertEqual(contentView.frame.size, expectedContentViewSize)
}
private func presentKeyboard() {
let keyboardFrame = CGRect(x: 0, y: window.bounds.height - keyboardHeight, width: window.bounds.width, height: keyboardHeight)
let initialSafeAreaSize = rootView.bounds.inset(by: rootView.safeAreaInsets).size
let initialBottomInset = scrollView.adjustedContentInset.bottom
// A test keyboard frame must be injected here because keyboard notifications will
// not be generated when a first responder is assigned within a test.
let keyboardFrameEvent = KeyboardFrameEvent(keyboardFrame: keyboardFrame, duration: 0.35)
scrollingContentViewManager.keyboardObserver?.testKeyboardFrameEvent(keyboardFrameEvent)
let finalSafeAreaSize = CGSize(width: initialSafeAreaSize.width, height: initialSafeAreaSize.height - (keyboardHeight - initialBottomInset))
XCTAssertEqual(contentView.frame.size, finalSafeAreaSize)
}
}