Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refine UIKit to SwiftUI Measurement Strategies #162

Conversation

brynbodayle
Copy link
Contributor

@brynbodayle brynbodayle commented Feb 15, 2024

Change summary

  • fixes an issue when sizing bridged UILabel's to SwiftUI where the view would always take up the proposed size
  • refactors sizing strategies for simplification and reducing unnecessary measurement
  • added sizing strategies demo code

How was it tested?

How did you verify that this change accomplished what you expected? Add more detail as needed.

  • Wrote automated tests
  • Built and ran on the iOS simulator
  • Built and ran on a device

Simulator Screen Recording - iPhone 6s - 2024-02-15 at 14 29 48

Pull request checklist

All items in this checklist must be completed before a pull request will be reviewed.

  • Risky changes have been put behind a feature flag, e.g. CollectionViewConfiguration
  • Added a CHANGELOG.md entry in the "Unreleased" section for any library changes

@brynbodayle brynbodayle added the bug Something isn't working label Feb 15, 2024
@brynbodayle brynbodayle force-pushed the bb-update-swiftui-measurement-strategies branch from ae075d7 to 54c3a04 Compare February 15, 2024 22:35
@brynbodayle brynbodayle force-pushed the bb-update-swiftui-measurement-strategies branch from 54c3a04 to 3226ed0 Compare February 16, 2024 22:44
@brynbodayle brynbodayle force-pushed the bb-update-swiftui-measurement-strategies branch 5 times, most recently from 8519841 to 8a287ff Compare February 21, 2024 00:48
@brynbodayle brynbodayle force-pushed the bb-update-swiftui-measurement-strategies branch from 8a287ff to 2ca6dc4 Compare February 21, 2024 00:52
return nsView.measuredFittingSize
}
#endif
}
#endif

#if swift(>=5.7.1) // Proxy check for being built with the iOS 15 SDK
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was getting a build error until I bumped this swift version up

// `UILabel.preferredMaxLayoutWidth`.
let intrinsicSize = content.systemLayoutFittingIntrinsicSize()

// If the view has a intrinsic width and contains a double layout pass subview, give it the
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this extra measurement pass didn't seem necessary in theory or in practice

@nonobjc
fileprivate func containsDoubleLayoutPassSubviews() -> Bool {
#if os(macOS)
return false
#else
var contains = false
if let label = self as? UILabel, label.preferredMaxLayoutWidth > 0 {
if let label = self as? UILabel, label.numberOfLines != 1 {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can simply check if the label is configured to show multiple lines. No need depending on an extra layout pass to determine if the label can wrap.

@brynbodayle brynbodayle force-pushed the bb-update-swiftui-measurement-strategies branch 3 times, most recently from 7c76ca3 to b331cfe Compare February 23, 2024 23:21
@brynbodayle brynbodayle force-pushed the bb-update-swiftui-measurement-strategies branch from b331cfe to 2a355a1 Compare March 1, 2024 19:50
@brynbodayle brynbodayle force-pushed the bb-update-swiftui-measurement-strategies branch from 2a355a1 to 35215ce Compare March 1, 2024 19:58
@brynbodayle brynbodayle marked this pull request as ready for review March 1, 2024 19:58
@brynbodayle
Copy link
Contributor Author

This is ready for review now. Happo diffs look more correct when testing in the Airbnb app.

/// - If the view contains `UILabel` subviews that require a double layout pass as determined by
/// a `preferredMaxLayoutWidth` that's greater than zero after a layout, then the view will
/// default to `intrinsicHeightProposedWidth` to allow the labels to wrap.
/// - If the view contains `UILabel` subviews that require a double layout pass as determined by support multiple lines of text
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wording is a little messed up

Copy link
Contributor

@bryankeller bryankeller left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This stuff is so tricky - thanks for figuring it out!

@@ -22,6 +22,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
approach to resolve an issue that could cause collection view cells to layout with
unexpected dimensions
- Made new layout-based SwiftUI cell rendering option the default.
- Fixed an issue where a UIKit view bridged to SwiftUI that wraps would always take up the proposed
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we fixed it for AppKit too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TBD, waiting for some updates from @calda since Epoxy iOS doesn't officially support native macOS, but Lottie iOS pulls in the code and uses it directly for macOS.

extension ProposedViewSize {
/// Creates a size by capping infinite values to a significantly large value and replacing `nil`s
/// with `UIView.noIntrinsicMetric`
var viewTypeValue: CGSize {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really understand the "viewType" part of this name

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a comment to clarify

Copy link
Contributor

@thedrick thedrick left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

awesome work @brynbodayle

@brynbodayle brynbodayle merged commit fb869c4 into airbnb:master Mar 1, 2024
9 checks passed
@brynbodayle brynbodayle deleted the bb-update-swiftui-measurement-strategies branch March 1, 2024 23:19
@calda calda mentioned this pull request Mar 7, 2024
3 tasks
brynbodayle added a commit to brynbodayle/epoxy-ios that referenced this pull request Mar 14, 2024
brynbodayle added a commit that referenced this pull request Mar 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants