Docsv3.31.0
Back to API Reference
Class

KeyboardAvoidanceBehavior

sealed

Keeps the field (or any assigned target) visible above the software keyboard by translating it upward, animated in lockstep with the system keyboard — the classic "lift the form so the keyboard doesn't cover it" reaction.

Remarks

Translation moves anchoredPosition.y, so the target must not be driven by a layout group on that axis — point target at the popup / panel root, not a layout child (for layout-driven forms a resize or scroll reaction fits better). On platforms without frame-synced keyboard animation (iOS, Android <30, WebGL) the motion is driven client-side from the event's timing metadata, or fallbackDuration / fallbackEasing when the platform provides none.
public RectTransform Target{ get; set }

Rect translated to remain visible; null resolves the field's clipping parent.

public float KeyboardPadding{ get; set }

Physical gap retained above the keyboard, measured in density-independent pixels.

public bool OnlyWhenOverlapping{ get; set }

Whether an already visible target remains at its authored position.

public float FallbackDuration{ get; set }

Client-side animation duration used when the platform supplies no timing.

public KeyboardEasing FallbackEasing{ get; set }

Client-side easing used when the platform supplies no animation curve.

protected bool IsAnimating{ get }

Whether the reaction is mid client-side animation and still needs OnKeyboardUpdate ticks — including an exit animation started in OnFocusLost that must finish after focus is already gone. The base keeps the per-frame tick alive while this holds, then releases it, so the field animates to rest rather than snapping.

protected override void OnFocusGained()

The owning field gained focus. Capture any baseline state to restore on focus loss. If the keyboard is already up (switching fields under an open keyboard) a synthetic OnKeyboardWillChangeFrame follows immediately so the reaction can position itself.

protected override void OnFocusLost()

The owning field lost focus. Restore any baseline state captured in OnFocusGained.

protected override void OnKeyboardWillShow(KeyboardEvent e)

System is about to show the keyboard; e carries the destination rect plus animation duration/easing. Start any client-driven animation here.

protected override void OnKeyboardWillChangeFrame(KeyboardEvent e)

Keyboard frame changed without a hide/show cycle — iPad split-keyboard toggle, hardware keyboard connect/disconnect, rotation, language switch, or a field switch under an open keyboard.

protected override void OnKeyboardWillHide(KeyboardEvent e)

System is about to hide the keyboard. Start any client-driven hide animation here.

protected override void OnKeyboardAnimationProgress(KeyboardEvent e)

Per-frame system animation fraction, only on platforms reporting frame-synced animation (KeyboardEvent.hasFrameSyncedAnimation = Android API 30+). Elsewhere this never fires — animate from OnKeyboardUpdate instead.

protected override void OnKeyboardDidShow(KeyboardEvent e)

The show animation finished and the keyboard is fully visible.

protected override void OnKeyboardDidHide(KeyboardEvent e)

The hide animation finished and the keyboard is fully gone.

protected override void OnKeyboardUpdate(float deltaTime)

Per-frame tick (unscaled delta) while focused, plus the tail of a client-side exit animation after focus loss (as long as IsAnimating holds). Drive client-side animation here on platforms without frame-synced progress (iOS, Android <30, WebGL).