<!--
{
  "availability" : [
    "iOS: -",
    "iPadOS: -",
    "macCatalyst: -",
    "tvOS: -",
    "visionOS: -",
    "watchOS: -"
  ],
  "documentType" : "symbol",
  "framework" : "UIKit",
  "identifier" : "/documentation/UIKit/UITextAttachmentViewProviderReusePolicy",
  "metadataVersion" : "0.1.0",
  "role" : "Structure",
  "symbol" : {
    "kind" : "Structure",
    "modules" : [
      "UIKit"
    ],
    "preciseIdentifier" : "c:@E@UITextAttachmentViewProviderReusePolicy"
  },
  "title" : "UITextAttachmentViewProviderReusePolicy"
}
-->

# UITextAttachmentViewProviderReusePolicy

An option set that controls whether a text view reuses attachment view providers when scrolling or editing.

```
struct UITextAttachmentViewProviderReusePolicy
```

## Overview

By default, when content scrolls out of the visible area, `UITextView` removes the attachment view from the view hierarchy. [`NSTextAttachmentViewProvider`](/documentation/UIKit/NSTextAttachmentViewProvider) itself persists, but the text view may recreate its view when the content returns. Editing the paragraph containing an attachment has a more significant effect: Because [`NSTextLayoutFragment`](/documentation/UIKit/NSTextLayoutFragment) is immutable, the text view replaces the existing fragment, discarding and recreating any [`NSTextAttachmentViewProvider`](/documentation/UIKit/NSTextAttachmentViewProvider) instances it holds. For attachments with views like a media player, a drawing canvas, or a focused control, either behavior can cause views to flicker or lose their state.

To reuse providers instead of recreating them, register a reuse policy for your view provider subclass:

```swift
textView.register(
    [.onScrollingOutOfViewport, .onEditingInlineParagraphs],
    forTextAttachmentViewProviderType: MyAttachmentViewProvider.self
)
```

You can use [`onScrollingOutOfViewport`](/documentation/UIKit/UITextAttachmentViewProviderReusePolicy/onScrollingOutOfViewport) or [`onEditingInlineParagraphs`](/documentation/UIKit/UITextAttachmentViewProviderReusePolicy/onEditingInlineParagraphs) on their own, or combine them to cover both scrolling and editing. Once registered, the reuse policy applies to every instance of that view provider subclass for as long as the text view exists.

Use [`onScrollingOutOfViewport`](/documentation/UIKit/UITextAttachmentViewProviderReusePolicy/onScrollingOutOfViewport) to keep an attachment view in the view hierarchy when its content scrolls out of the visible area — when it scrolls back into view, the text view reuses the existing view instead of recreating it, preserving states like first responder status and media playback position. Use [`onEditingInlineParagraphs`](/documentation/UIKit/UITextAttachmentViewProviderReusePolicy/onEditingInlineParagraphs) to keep a provider when someone edits the paragraph containing it; on the next layout pass, the text view reuses it instead of creating a new one, which prevents visible flicker when someone types near an attachment.

## Topics

### Creating a reuse policy

[`init(rawValue: UInt)`](/documentation/UIKit/UITextAttachmentViewProviderReusePolicy/init(rawValue:))

### Specifying reuse policy options

[`static var onScrollingOutOfViewport: UITextAttachmentViewProviderReusePolicy`](/documentation/UIKit/UITextAttachmentViewProviderReusePolicy/onScrollingOutOfViewport)

[`static var onEditingInlineParagraphs: UITextAttachmentViewProviderReusePolicy`](/documentation/UIKit/UITextAttachmentViewProviderReusePolicy/onEditingInlineParagraphs)

## Relationships

### Conforms To

[`RawRepresentable`](/documentation/Swift/RawRepresentable)

[`SendableMetatype`](/documentation/Swift/SendableMetatype)

[`ExpressibleByArrayLiteral`](/documentation/Swift/ExpressibleByArrayLiteral)

[`BitwiseCopyable`](/documentation/Swift/BitwiseCopyable)

[`OptionSet`](/documentation/Swift/OptionSet)

[`Sendable`](/documentation/Swift/Sendable)

[`Equatable`](/documentation/Swift/Equatable)

[`SetAlgebra`](/documentation/Swift/SetAlgebra)

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)