Disable UISearchBar search icon and placeholder text animation on becomeFirstResponder
Asked Answered
L

6

8

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.

Lecce answered 6/11, 2013 at 17:15 Comment(0)
S
3

@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() }
  }
}
Sharanshard answered 6/12, 2016 at 18:13 Comment(0)
H
0

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:@""];
Hatley answered 6/11, 2013 at 17:36 Comment(4)
why are you setting the place holder text, use the textHatley
I need the placeholder text so when the user starts typing, it goes away and is replaced with the typed character. For the search icon, I can animate it to a specified position using [setPositionAdjustment:forSearchBarIcon] but it still does so using an animation.Lecce
placeholder text will be center aligned. I don't think you can change thatHatley
No, the placeholder text is center aligned when the textField is inactive, then animates to left-aligned when it becomes firstResponder, then when the user presses a key, it disappears. It then reappears when the textField is empty.Lecce
U
0

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.

Utopia answered 30/6, 2014 at 16:34 Comment(0)
E
0

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:@""];
Encephaloma answered 22/7, 2015 at 6:43 Comment(0)
D
0
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)
}
Dacoity answered 15/8, 2018 at 20:14 Comment(0)
A
-1

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
Antechamber answered 17/3, 2014 at 17:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.