Question

I have a UISearchBar that I want to display when the user taps a button. In the buttonPress method, I create the searchBar and add it as a subview, then call [searchBar becomeFirstResponder]. If I take out this becomeFirstResponder call, the search icon and placeholder text appear in the center of the textField.

enter image description here

Then, when the search bar becomes first responder, both animate to be left-aligned.

enter image description here

Because I'm doing both actions successively, I'm getting a weird animation where the icon & placeholder animate from (0,0).

How can I disable this animation, so that I can simply add the second image to my view?

EDIT:

I got the placeholder text to display correctly by using

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    [searchBar setPlaceholder:@"Type Here to Search"];
}

I can move the search icon using [searchBar setPositionAdjustment:UIOffsetMake(x, y) forSearchBarIcon:UISearchBarIconSearch];, but the changes are still applied within the animation.

No correct solution

OTHER TIPS

@user3429963 is definitely looking in the right direction. Here's what worked for me:

searchBar.becomeFirstResponder()
searchBar.removeLayerAnimationsRecursively()

where removeLayerAnimationsRecursively() is a function which removes animations from the view layer and its subviews' layers recursively:

extension UIView {

  func removeLayerAnimationsRecursively() {
    layer.removeAllAnimations()
    subviews.forEach { $0.removeLayerAnimationsRecursively() }
  }
}

It's a hack but if you can't live without it.

EDIT: If you want the placeholder text too, you can do this.

set the placeholder text to @" " initially, and

[self.searchBar setText:@"Place holder"];

[self.searchBar setPlaceholder:@"Place holder"];

[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor grayColor]];

and after it becomes first Responder,

[self.searchBar setText:@""];

Certain number of spaces at the end of placeholder solved for me the issue. Note, it should be accurate number of spaces - not less and not more.

Use this code to hide icon of search bar :-

[searchBar setImage:[UIImage imageNamed:@"searchIcon.jpg"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];

Take transparent image with searchIcon.jpg name and your icon hide its work for me

[searchBar setText:@""];
override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    UIView.setAnimationsEnabled(false)
    self.searchController?.isActive = true
    self.searchController?.searchBar.becomeFirstResponder()
}

func didPresentSearchController(_ searchController: UISearchController) {
    searchController.searchBar.becomeFirstResponder()
    UIView.setAnimationsEnabled(true)
}

it works for me (testing only on ios7)

@interface SearchBar : UISearchBar
@end

@implementation SearchBar
- (void)layoutSubviews
{
    [super layoutSubviews];

    UITextField *textField = (UITextField *)[self firstSubviewOfClass:[UITextField class]];
    UIImageView *imageView = (UIImageView *)[textField firstSubviewMatchingBlock:^BOOL(UIView *obj) {
        return [obj isKindOfClass:[UIImageView class]] && obj.layer.animationKeys.count;
    }];
    [imageView.layer removeAllAnimations];
    UILabel *label = (UILabel *)[self firstSubviewOfClass:[UILabel class]];
    [label.layer removeAllAnimations];
}
@end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top