Skip to content

Commit 24bf675

Browse files
committed
fix(ios): defer fit assignment until MTKView is ready in experimental backend
RiveUIView creates its MTKView lazily during the first layout pass. Setting rive.fit immediately after creating the view had no effect because the drawable didn't exist yet. Store the desired fit as pendingFit and apply it in layoutSubviews once the MTKView is present.
1 parent fb86bf0 commit 24bf675

1 file changed

Lines changed: 17 additions & 7 deletions

File tree

ios/new/RiveReactNativeView.swift

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
@_spi(RiveExperimental) import RiveRuntime
1+
import RiveRuntime
22
import NitroModules
33
import UIKit
4+
import MetalKit
45

56
enum ExperimentalBindData {
67
case none
@@ -27,9 +28,23 @@ class RiveReactNativeView: UIView {
2728
private var isViewReady = false
2829
private var configTask: Task<Void, Never>?
2930
private var isPaused = false
31+
private var pendingFit: RiveRuntime.Fit?
3032

3133
var autoPlay: Bool = true
3234

35+
override func layoutSubviews() {
36+
super.layoutSubviews()
37+
applyPendingFitIfMTKViewReady()
38+
}
39+
40+
// https://github.com/rive-app/rive-nitro-react-native/pull/231
41+
private func applyPendingFitIfMTKViewReady() {
42+
guard let fit = pendingFit, let rive = riveInstance,
43+
riveUIView?.subviews.contains(where: { $0 is MTKView }) == true else { return }
44+
rive.fit = fit
45+
pendingFit = nil
46+
}
47+
3348
func awaitViewReady() async -> Bool {
3449
if !isViewReady {
3550
await withCheckedContinuation { continuation in
@@ -93,14 +108,9 @@ class RiveReactNativeView: UIView {
93108

94109
RCTLog("[RiveReactNativeView] Rive instance created successfully")
95110
self.riveInstance = rive
96-
RCTLog("[RiveReactNativeView] Setting up RiveUIView...")
97111
self.setupRiveUIView(with: rive)
98-
RCTLog("[RiveReactNativeView] RiveUIView setup complete")
99112

100-
// Set fit after view is in the hierarchy — passing fit to
101-
// the Rive() constructor breaks .layout mode because the
102-
// MTKView drawable isn't ready yet at construction time.
103-
rive.fit = config.fit
113+
self.pendingFit = config.fit
104114

105115
if config.autoPlay {
106116
self.isPaused = false

0 commit comments

Comments
 (0)