2010-11-24 10 views
43

Estoy interesado en saber cómo puedo cambiar el tamaño de la vista al utilizar UIModalPresentationFormSheet de modalPresentationStyle, parece que tiene un tamaño fijo, así que me preguntaba si alguien logró manipular la vista emergente del tamaño perspectiva.UIModalPresentationFormSheet cambio de tamaño vista

Así que hay UIModalPresentationFormSheet con un tamaño de vista fijo o vistas completas y estoy buscando algo intermedio.

+2

posible duplicado de [Cómo cambiar el tamaño de un UIPresentationFormSheet?] (Http://stackoverflow.com/questions/2457947/how-to-resize-a-uipresentationformsheet) – JosephH

Respuesta

71
MyModalViewController *targetController = [[[MyModalViewController alloc] init] autorelease]; 

targetController.modalPresentationStyle = UIModalPresentationFormSheet; 

targetController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; 

[self presentModalViewController:targetController animated:YES]; 

// it is important to do this after presentModalViewController:animated: 
targetController.view.superview.bounds = CGRectMake(0, 0, 200, 200); 
+48

Esta no es una buena solución porque agrega algún tipo de desenfoque al título del elemento de navegación. Este es mejor: targetController.view.superview.bounds = CGRectMake (0, 0, 200, 200); Todavía es el centro, no hay necesidad de hacer ... superview.center. –

+0

Gracias @BorutTomazin que resolvió mi problema – emmandroid

+0

El desenfoque es causado por la colocación de subpixel. Mi respuesta anterior corrige esto. targetController.view.superview.center = GPointMake (roundf (self.view.center.x), roundf (self.view.center.y)); –

4

que son capaces de ajustar el marco de una vista modal tras presentarse:

Probado en iOS 5.1 a 6.1, utilizando XCode 4,62

MyModalViewController *targetController = [[[MyModalViewController alloc] init] autorelease]; 
targetController.modalPresentationStyle = UIModalPresentationFormSheet; 
targetController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; //transition shouldn't matter 
[self presentModalViewController:targetController animated:YES]; 
targetController.view.superview.frame = CGRectMake(0, 0, 200, 200);//it's important to do this after presentModalViewController 
targetController.view.superview.center = GPointMake(roundf(self.view.center.x), roundf(self.view.center.y));//self.view assumes the base view is doing the launching, if not you might need self.view.superview.center etc. 

actualización El iOS 6.0 vista de presentación controlador preferido método también funciona correctamente:

- (void)presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion 
+0

gracias! MyModalViewController * targetController = [[[MyModalViewController alloc] init] liberación automática]; targetController.modalPresentationStyle = UIModalPresentationFormSheet; targetController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; // la transición no debería importar [self presentModalViewController: targetController animated: YES]; targetController.view.superview.frame = CGRectMake (0, 0, 200, 200); // es importante hacerlo después de presentModalViewController targetController.view.superview.center = self.view.center; – tosi

2

Sólo a medida solución Fatos (gran uno) Si su están creando el controlador de vista usando un archivo .xib después de la alloc initWithNibName podría almacenar el marco de vista:

CGRect myFrame = targetController.view.frame; 
... 
targetController.view.superview.bounds = myFrame; 

Y luego utilizarlo para superview.bounds, por lo que se usará el tamaño de la vista en .xib y podría cambiar el tamaño más visualmente.

-1

que poner este código en el controlador de vista forma de hoja:

-(void) viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 
    self.view.superview.bounds = CGRectMake(0, 0, 540, 500); // your size here 
} 

Tenga en cuenta que se produce el cambio de tamaño suficientemente temprano que la animación de la presentación es correcta.

+1

No funcionó para mí. – WebSeed

4

En ios8 y anteriores trabajos:

AboutViewController * _aboutViewController = [[AboutViewController alloc] init]; 
    _aboutViewController.modalPresentationStyle = UIModalPresentationFormSheet; 
    if(IS_IOS8) 
    { 
     _aboutViewController.preferredContentSize = CGSizeMake(300, 300); 
    } 
    [self presentViewController:_aboutViewController animated:YES completion:nil]; 

En AboutViewController.m

- (void)viewWillLayoutSubviews{ 
    [super viewWillLayoutSubviews]; 

    if(!IS_IOS8) 
    { 
     self.view.superview.bounds = CGRectMake(0, 0, 300, 300); 
    } 
} 

IS_IOS8

#define IS_IOS8 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8) 

En iOS 8 también se puede utilizar UIPresentationController que le da más opciones de personalización .

3

Para iOS 8, simplemente implemente el método de delegado (CGSize) preferredContentSize en cada controlador de vista. Debería resolver todo el problema de tamaño.

+0

El método (CGSize) preferredContentSize está disponible en iOS7 – Ash

+0

Esto funciona al principio, pero luego el modal vuelve a su tamaño predeterminado original. – fatuhoku

Cuestiones relacionadas