I’m trying to emulate the behaviour of the address bar in iOS Safari – that is, if there’s text in the address bar when it’s tapped, it is selected. Much in the same way most desktop browsers work, too. I don’t want to CLEAR it on selection, merely select what’s inside, so that if the user then begins typing, it’ll clear, but the user also has the ability to edit the existing string if they want to.
I’ve tried the following, it finds the UISearchBarTextField
inside the subviews under both iOS7 and iOS6, but calling selectAll
on it doesn’t seem to have any effect. I’ve tried changing its text
property, that works fine, it’s just selectAll
that isn’t happy.
I’ve also tried changing the argument to selectAll:
to self
and a couple other things, no change, just doesn’t wanna select the text in there!
Any thoughts? Here’s my code:
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
NSArray *subviews = [UIDevice isRunningiOS7OrAbove]
? [[searchBar.subviews objectAtIndex:0] subviews]
: searchBar.subviews;
for (UIView *subview in subviews) {
if ([subview conformsToProtocol:@protocol(UITextInputTraits)]) {
UITextField *textField = (UITextField *)subview;
[textField selectAll:textField];
}
}
}
EDIT
I’ve also tried this, with no joy:
for (UIView *subview in subviews) {
if ([subview conformsToProtocol:@protocol(UITextInputTraits)]) {
UITextField *textField = (UITextField *)subview;
[textField setSelectedTextRange:[textField textRangeFromPosition:[textField beginningOfDocument]
toPosition:[textField endOfDocument]]];
}
}
Ran into the same problem. The thing that helped me was delaying the
selectAll
by a short period of time.I made this extension for
UISearchBar
then I simply call this method on my searchbar with a short delay.
Swift 2
Swift 3 Xcode 8 beta 4
WARNING: This approach is far from clear solution, but it works fine. The short delay shouldn't cause any trouble since it's just a selecting action on a textfield. Though accessing the
searchField
property of yourUISearchBar
can lead to app rejection!You can use:
[UITextField setSelectedTextRange:(UITextRange *)]
Now you can implement using
UISearchBar
...For me passing UITextField parameter didnt work. I am passing to selectAll method nil param so your line should look like: