2011-08-05 13 views
5

Estoy escribiendo una aplicación de iOS para iPad que requiere un diseño personalizado.Cuándo usar layoutSubview en iOS

El diseño de retrato y paisaje es totalmente diferente, por lo que no se puede resolver con UIAutoResizingMask.

Intento utilizar el método layoutSubview, pero detecté que la subvista de diseño se llama mucho (desde UIScrollView). ¿Cómo puedo reducir la llamada layoutSubview para optimizar el código, o debo llamar por mi cuenta cada vez que se gira el dispositivo.

Gracias.

Respuesta

3

El hecho de que layoutSubviews es llamado por un niño UIScrollView es muy desafortunado, pero hay un (feo) solución:

@interface MyClass : UIView { 
    BOOL reallyNeedsLayout_; 
} 
@end 

@implementation MyClass 

- (void)setNeedsLayout 
{ 
    [super setNeedLayout]; 
    reallyNeedsLayout_ = YES; 
} 

- (void)setFrame:(CGRect)rect 
{ 
    [super setFrame:rect]; 
    reallyNeedsLayout_ = YES; 
} 

- (void)layoutSubviews 
{ 
    if (!reallyNeedsLayout_) return; 
    reallyNeedsLayout_ = NO; 

    // Do layouting. 
} 
@end 

No es la mejor solución, pero parece que funciona razonablemente bien.

+0

hasta ahora, ¿cuál es la mejor solución para este señor ? –

0

Hablando por experiencia, yo personalmente solo ajustaría su diseño según las notificaciones de deviceDidRotateSelector. Tengo un método updatePortrait y un método updateLandscape y llamo a cualquiera que sea necesario.

4

Para métodos diferentes controladores de paisaje y diseño del retrato utilización vista como

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration; 
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation; 
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration; 

Si crea la vista personalizada en función de la orientación actual, comprobar esta orientación por UIDeviceOrientationDidChangeNotification notificación y escriben código apropiado.

en uno de los métodos init~:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didChangedOrientation:) name:UIDeviceOrientationDidChangeNotification object:nil]; 

Y la acción

- (void) didChangedOrientation:(NSNotification *)sender{ 
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; 
if (UIDeviceOrientationIsPortrait(orientation)){}} 
1

No deberías hacer cálculos costosos en layoutSubviews:

Cuestiones relacionadas