<!--
{
  "availability" : [
    "iOS: 26.0.0 -",
    "iPadOS: 26.0.0 -",
    "macCatalyst: 26.0.0 -",
    "tvOS: 26.0.0 -",
    "visionOS: 26.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "UIKit",
  "identifier" : "/documentation/UIKit/UISearchBarDelegate/searchBar(_:shouldChangeTextInRanges:replacementText:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "UIKit"
    ],
    "preciseIdentifier" : "c:objc(pl)UISearchBarDelegate(im)searchBar:shouldChangeTextInRanges:replacementText:"
  },
  "title" : "searchBar(_:shouldChangeTextInRanges:replacementText:)"
}
-->

# searchBar(_:shouldChangeTextInRanges:replacementText:)

```
optional func searchBar(_ searchBar: UISearchBar, shouldChangeTextInRanges ranges: [NSValue], replacementText: String) -> Bool
```

## Parameters

`searchBar`

The search bar asking the delegate

`ranges`

The ranges of the text that should be deleted before replacing

`replacementText`

The replacement text

## Return Value

Returns true if the text at the `ranges` should be replaced.

## Discussion\abstract Asks the delegate if the text at the specified `ranges` should be replaced with `text`.
\discussion If this method returns YES then the search bar will, at its own discretion, choose any one of the specified `ranges` of text and replace it with the specified `replacementText` before deleting the text at the other ranges. If the delegate does not implement this method then the `searchBar:shouldChangeTextInRange:replacementText:` method will be called and passed the union range instead. If the delegate also does not implement that method then YES is assumed.

---

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)