<!--
{
  "availability" : [
    "iOS: 13.0.0 -",
    "iPadOS: 13.0.0 -",
    "macCatalyst: 13.1.0 -",
    "tvOS: 13.0.0 -",
    "visionOS: 1.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "UIKit",
  "identifier" : "/documentation/UIKit/UIMenuBuilder/replaceChildren(ofMenu:from:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "UIKit"
    ],
    "preciseIdentifier" : "c:objc(pl)UIMenuBuilder(im)replaceChildrenOfMenuForIdentifier:fromChildrenBlock:"
  },
  "title" : "replaceChildren(ofMenu:from:)"
}
-->

# replaceChildren(ofMenu:from:)

Replaces the elements in a menu with the elements returned by the specified handler block.

```
func replaceChildren(ofMenu parentIdentifier: UIMenu.Identifier, from childrenBlock: ([UIMenuElement]) -> [UIMenuElement])
```

## Parameters

`parentIdentifier`

The identifier of the menu containing the children to replace.

`childrenBlock`

A handler that returns the menu elements that replace the children in the menu associated with `parentIdentifier`. This handler has the following parameter:

- `oldChildren`: The array of menu elements to replace.

## Discussion

Use this method to add, remove, and rearrange the children elements of a menu. For example, the following code listing uses this method to insert a Copy HTML menu element before Paste in the [`standardEdit`](/documentation/UIKit/UIMenu/Identifier-swift.struct/standardEdit) menu.

```swift
builder.replaceChildren(ofMenu: .standardEdit) { (oldChildren) -> [UIMenuElement] in
    // Find the index of Paste menu element.
    var indexOfPaste = 0
    for (index, menuElement) in oldChildren.enumerated() {
        if let keyCommand = menuElement as? UIKeyCommand {
            if keyCommand.action == #selector(UIResponderStandardEditActions.paste(_:)) {
                indexOfPaste = index
                break
            }
        }
    }
    
    // Create a Copy HTML menu element.
    let copyHTML = UIKeyCommand(title: "Copy as HTML",
                                action: #selector(copyHTML(_:)),
                                input: "c",
                                modifierFlags: [.control, .command])
    
    // Insert Copy HTML before the Paste menu element
    // if found; otherwise, insert Copy HTML at the
    // beginning of the array.
    var newChildren = oldChildren
    newChildren.insert(copyHTML, at: indexOfPaste)
    return newChildren
}
```

---

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)