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.
Then, when the search bar becomes first responder, both animate to be left-aligned.
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.
@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() }
}
}