2010-09-22 10 views
11

HI, tengo Parentview -> childviews múltiples. cuando se utiliza [bringSubviewToFront auto: childview] en parentview, funciona fine.but después de añadir grandchildview a childview, cuando uso [auto bringSubviewToFront: grandchildview]? En parentview, no funcionó ninguna ayuda por favor?bringSubviewToFront problema?

Respuesta

52

-[UIView bringSubviewToFront:] El método sólo funciona para dirigir a los niños, no nietos. Recuerde que la jerarquía de la vista es un árbol, y, normalmente, una vista de sólo conoce su "padre" (o supervista) y sus directos "niños" (o subvistas). Usted tendría que hacer algo como esto:

// First, get the view embedding the grandchildview to front. 
[self bringSubviewToFront:[grandchildview superview]]; 
// Now, inside that container view, get the "grandchildview" to front. 
[[grandchildview superview] bringSubviewToFront:grandchildview]; 
+2

trabajo fino ... gran código! –

+0

haciendo esto de forma recursiva sería aún mejor –

+1

@PizzaiolaGorgonzola Para los niveles de anidación arbitrarias, sí, un método recursivo sería el camino a seguir. Sin embargo, para este caso simple sería excesivo. – DarkDust

0

mi contribución con la versión rápida de la solución comprobada

self.bringSubviewToFront(self.grandchildview.superview!) 
self.grandchildview.superview!.bringSubviewToFront(self.grandchildview)