---
title: "replaceSubview(_:with:)"
framework: appkit
role: symbol
role_heading: Instance Method
path: "appkit/nsview/replacesubview(_:with:)"
---

# replaceSubview(_:with:)

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

## Declaration

```swift
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

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.

## See Also

### Adding and Removing Subviews

- [addSubview(_:)](appkit/nsview/addsubview(_:).md)
- [addSubview(_:positioned:relativeTo:)](appkit/nsview/addsubview(_:positioned:relativeto:).md)
- [removeFromSuperview()](appkit/nsview/removefromsuperview().md)
- [removeFromSuperviewWithoutNeedingDisplay()](appkit/nsview/removefromsuperviewwithoutneedingdisplay().md)
- [sortSubviews(_:context:)](appkit/nsview/sortsubviews(_:context:).md)
