Skip to content

Commit 93178a5

Browse files
zetasqShengqi Zhu
andauthored
Expose forceLayout in EpoxySwiftUIHostingView (#172)
* Expose `forceLayout` in EpoxySwiftUIHostingView * Add comments --------- Co-authored-by: Shengqi Zhu <shengqi.zhu@airbnb.com>
1 parent be1f8f0 commit 93178a5

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased](https://github.com/airbnb/epoxy-ios/compare/0.10.0...HEAD)
88

9+
### Added
10+
- Expose `forceLayout` in `EpoxySwiftUIHostingView` for updating the hosting view size from outside.
11+
912
### Changed
1013
- `AnyItemModel` now implements the `ErasedContentProviding` protocol.
1114
- Updated `ErasedContentProviding` protocol to use its type name instead of `Self` in the keys of its `EpoxyModelProperty` properties `contentProperty` and `isContentEqualProperty `.

Sources/EpoxyCore/SwiftUI/EpoxySwiftUIHostingView.swift

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -198,12 +198,9 @@ public final class EpoxySwiftUIHostingView<RootView: View>: UIView, EpoxyableVie
198198
// The view controller must be added to the view controller hierarchy to measure its content.
199199
addViewControllerIfNeededAndReady()
200200

201-
// We need to layout the view to ensure it gets resized properly when cells are re-used
202-
viewController.view.setNeedsLayout()
203-
viewController.view.layoutIfNeeded()
204-
205-
// This is required to ensure that views with new content are properly resized.
206-
viewController.view.invalidateIntrinsicContentSize()
201+
// In this method `setNeedsLayout` and `layoutIfNeeded` are called. We need to layout the view to ensure it gets resized properly when cells are re-used
202+
// In this method `invalidateIntrinsicContentSize` is called. This is required to ensure that views with new content are properly resized.
203+
forceLayout()
207204
}
208205

209206
public override func layoutMarginsDidChange() {
@@ -234,9 +231,7 @@ public final class EpoxySwiftUIHostingView<RootView: View>: UIView, EpoxyableVie
234231
// to be more common with top and bottom bars, since they can be laid out early during view
235232
// controller transitions. If this works well, we may make this the default behavior for all
236233
// SwiftUI views.
237-
viewController.view.setNeedsLayout()
238-
viewController.view.layoutIfNeeded()
239-
viewController.view.invalidateIntrinsicContentSize()
234+
forceLayout()
240235
} else {
241236
// Allow the layout margins update to fully propagate through to the SwiftUI View before
242237
// invalidating the layout.
@@ -246,6 +241,14 @@ public final class EpoxySwiftUIHostingView<RootView: View>: UIView, EpoxyableVie
246241
}
247242
}
248243

244+
/// Force relayout the UIHostingView and invalidate its intrinsic content size.
245+
/// This is used for updating the view size manually.
246+
public func forceLayout() {
247+
viewController.view.setNeedsLayout()
248+
viewController.view.layoutIfNeeded()
249+
viewController.view.invalidateIntrinsicContentSize()
250+
}
251+
249252
public func handleWillDisplay(animated: Bool) {
250253
guard state != .appeared, window != nil else { return }
251254
transition(to: .appearing(animated: animated))

0 commit comments

Comments
 (0)