2010-03-23 14 views
7

Estoy tratando de transformar un UISearchBar, como en Mobile Safari: toque en el campo de búsqueda y crece mientras el campo de ubicación se reduce.UIView Animación anima la posición pero no el ancho

Mi animación actual para modificar el ancho y la posición del campo de búsqueda solo anima la posición: justo antes de que se deslice hacia el lugar correcto, simplemente se ajusta al ancho correcto. Aquí está mi código:

[UIView beginAnimations:@"searchGrowUp" context:nil]; 
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn]; 
[UIView setAnimationDuration:0.5f]; 
[UIView setAnimationDelegate:self]; 

CGFloat findFieldWidth = findField.frame.size.width; 
CGFloat urlFieldWidth = urlField.frame.size.width; 
CGRect findFieldFrame = findField.frame; 
CGRect urlFieldFrame = urlField.frame; 

findFieldFrame.origin.x = findFieldFrame.origin.x - 150.0f; 
findFieldFrame.size.width = findFieldWidth + 150.0f; 
urlFieldFrame.size.width = urlFieldWidth - 150.0f; 

urlField.frame = urlFieldFrame; 
findField.frame = findFieldFrame; 

[UIView commitAnimations]; 

He modificado ligeramente este código en aras de presentar aquí, pero espero que esto da la esencia.

¡Se agradecerán todas las conjeturas sobre por qué sucede esto!

Cheers, Aaron.

+0

¡Esto mejor no sea para Macinsite! :> –

Respuesta

12

lo he descubierto gracias a este post: Changing the size of the UISearchBar TextField?

Resulta que el contenido de un UISearchBar no redimensionar adecuadamente junto con la capa exterior. Por lo tanto, debe llamar a -layoutSubviews: dentro del bloque de animación después de establecer el marco en la barra de búsqueda. Entonces el bloque termina como:

[findField setFrame:CGRectMake(findField.bounds.origin.y, findField.bounds.origin.y, findFieldWidth, findField.bounds.size.height)]; 
[findField layoutSubviews]; 

[UIView commitAnimations]; 

Props to Nick Farina!

1

Lo siento, esto es un poco viejo, pero me encontré con un problema similar. No quería usar layoutSubviews porque la documentación dice que no debe llamar a ese método directamente.

Lo que hice para resolver mi problema fue llamar sizeToFit en la subvista dentro del bloque de animación.

+0

No funciona para mí - iOS 5, iPhone 4S. –

+0

Tuve el mismo problema con una UITextView. Esta es la única respuesta que me solucionó el problema. – Fireandlight27

7

Tuve el mismo problema al intentar animar el ancho de UISearchBar.

En iOS 4 y posteriores, he encontrado que el uso de UIViewAnimationOptionLayoutSubviews como una opción para

+[UIView animateWithDuration:delay:options:animations:completion:] 

arreglado.

+0

Esta opción (y su respuesta) son bienvenidas ... Gracias – MiKL

3

La respuesta aceptada funciona pero de acuerdo con los documentos de Apple "no debe llamar a este método". La solución limpia es utilizar layoutIfNeeded método:

[UIView animateWithDuration:UINavigationControllerHideShowBarDuration 
         delay:0.f 
        options:UIViewAnimationOptionCurveEaseInOut 
       animations:^{ 

    [searchBar setFrame: ... 
    [searchBar layoutIfNeeded]; 

    ... 

Enjoy!

+0

Esta respuesta resolvió este problema. Gracias – Vinh

Cuestiones relacionadas