<!--
{
  "availability" : [
    "macOS: -"
  ],
  "documentType" : "symbol",
  "framework" : "AppKit",
  "identifier" : "/documentation/AppKit/NSView/replaceSubview(_:with:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "AppKit"
    ],
    "preciseIdentifier" : "c:objc(cs)NSView(im)replaceSubview:with:"
  },
  "title" : "replaceSubview(_:with:)"
}
-->

# replaceSubview(_:with:)

Replaces one of the view’s subviews with another view.

```
func replaceSubview(_ oldView: NSView, with newView: NSView)
```

## Parameters

`oldView`

The view to be replaced by `newView`. May not be `nil`.

`newView`

The view to replace `oldView`. May not be `nil`.

## Discussion

This method does nothing if `oldView` is not a subview of the view.

Neither `oldView` nor `newView` may be `nil`, and the behavior is undefined if either of these parameters is `nil`.

This method causes `oldView` to be released; if you plan to reuse it, be sure to retain it before sending this message and to release it as appropriate when adding it as a subview of another NSView.

Calling this method also removes any constraints associated with `oldView` and its subtree.

---

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)