CoursesCompose Multiplatform, one screen from state to pixels

Puzzle 2 · 3 of 4

Which side hosts the other?

Place a shared bookmarks screen in an iOS app, then put an Apple text field inside the shared screen.

All puzzles in this course

The problem

Our shared bookmarks screen already runs on Android. On iOS it sits under the app’s existing navigation bar. The search field uses UIKit because it needs a behavior specific to Apple’s text field. Which object hosts which?

See it

Which side hosts the other? iOS app Screen host Shared screen Apple field

Scroll sideways to see the full diagram.

  1. An existing iOS app owns the navigation around bookmarks.

    Step 1 of 4
  2. Its navigation presents a controller which hosts the shared UI.

    Step 2 of 4
  3. Compose arranges the contents inside that controller.

    Step 3 of 4
  4. The shared region can in turn host one native text field.

    Step 4 of 4

The building block

A hosting boundary is where one UI system gives a region to the other. There are two different directions in this example.

NeedWhat to useOwner of the surrounding region
Show the shared bookmarks in an existing iOS appComposeUIViewControllerUIKit or a SwiftUI wrapper around that controller
Put a native search field inside the shared screenUIKitViewCompose

A view controller manages an area of UIKit UI. The controller returned by ComposeUIViewController hosts the shared composables. The rendering phases still run inside that shared region. SwiftUI can show that controller using UIViewControllerRepresentable, an adapter that lets SwiftUI display a UIKit controller.

Within the shared area, UIKitView can host a UITextField, Apple's text input view. Its factory creates the view. Its update callback can copy the latest shared value onto that existing view when the displayed text differs. User edits go in the other direction, through a native callback that updates the shared value.

For a field driven by shared state, make both directions explicit: user edit → shared value → displayed text. If you change the shared value through another action, such as Clear, the native field needs the update path to show the change.

Go deeperA reset while the keyboard is open
The field writes edits to shared state, but a tap on Clear changes only that state. Which missing edge can leave old text on screen?

The native edit callback handles the other direction. The update callback has to copy the cleared shared value into the existing native field.

Back to the main puzzle

Check yourself

An existing UIKit navigation controller must present a shared screen. Which object belongs in its screen stack?

Not quite. The host needs an iOS controller that contains the shared UI. Re-read that part ↑

Not quite. UIKitView embeds a native view in the other direction. Re-read that part ↑

Right. Start from whoever owns the surrounding region.

Why can a Clear button fail to clear the native field even though it clears shared state?

Right. State has to travel back from Compose to the native view.

Not quite. That callback reports edits from the view. Clear already changed shared state. Re-read that part ↑

Not quite. Creation and state updates are separate steps. Re-read that part ↑

The latest value changes while the native field still exists. What should use that value?

Right. Keep creation and synchronization separate.

Not quite. The navigation controller owns placement of the screen, not the field text. Re-read that part ↑

Not quite. The view needs a later update, after its initial creation. Re-read that part ↑

Sources

What you now know

A shared screen can sit inside an iOS view controller. Inside that screen, a native view can be embedded with distinct creation and update steps.