---
title: "insertChild(_:at:)"
framework: appkit
role: symbol
role_heading: Instance Method
path: "appkit/nsviewcontroller/insertchild(_:at:)"
---

# insertChild(_:at:)

Inserts a specified child view controller into the children array at a specified position.

## Declaration

```swift
func insertChild(_ childViewController: NSViewController, at index: Int)
```

## Parameters

- `childViewController`: The child view controller to add to the doc://com.apple.appkit/documentation/AppKit/NSViewController/children array.
- `index`: The index in the doc://com.apple.appkit/documentation/AppKit/NSViewController/children array at which to insert the child view controller. This value must not be greater than the count of elements in the array.

## Discussion

Discussion You should instead use the addChild(_:) method unless you want to perform work on child view controllers as you add them. In that case, override this method to perform that work. If a child view controller has a different parent when you call this method, the child is first be removed from its existing parent by calling the child’s removeFromParent() method.

## See Also

### Managing Child View Controllers in a Custom Container

- [addChild(_:)](appkit/nsviewcontroller/addchild(_:).md)
- [children](appkit/nsviewcontroller/children.md)
- [transition(from:to:options:completionHandler:)](appkit/nsviewcontroller/transition(from:to:options:completionhandler:).md)
- [removeChild(at:)](appkit/nsviewcontroller/removechild(at:).md)
- [removeFromParent()](appkit/nsviewcontroller/removefromparent().md)
- [preferredContentSizeDidChange(for:)](appkit/nsviewcontroller/preferredcontentsizedidchange(for:).md)
