Eu estou tentando exibir um controlador de modo restrito como um UIPresentationFormSheet. A exibição aparece, mas não consigo redimensioná-la. Meu XIB tem a altura e largura adequadas, mas parece ser substituído quando eu o chamo assim:
composeTweetController = [[ComposeTweet alloc] initWithNibName:@"ComposeTweet" bundle:nil]; composeTweetController.modalPresentationStyle = UIModalPresentationFormSheet; [self presentModalViewController:composeTweetController animated:TRUE];
Alguma ideia? Eu estou usando o iPhone SDK 3.2 beta 4
Você pode ajustar o quadro de uma vista modal depois de apresentá-lo:
Testado no iOS 5.1 – 7.1
MyModalViewController *targetController = [[[MyModalViewController alloc] init] autorelease]; targetController.modalPresentationStyle = UIModalPresentationFormSheet; targetController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; //transition shouldn't matter [self presentViewController:targetController animated:YES completion:nil]; if(floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1){ targetController.view.superview.frame = CGRectInset(targetController.view.superview.frame, 100, 50); }else{ targetController.view.frame = CGRectInset(targetController.view.frame, 100, 50); targetController.view.superview.backgroundColor = [UIColor clearColor]; }
Aqui está um método que funciona no iOS7 , bem como iOS5 e iOS6: (arco)
-(void)presentController:(UIViewController*)controller fromRootController:(UIViewController*)rootController withSize:(CGSize)size { UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:controller]; nav.modalTransitionStyle = UIModalTransitionStyleCoverVertical; nav.modalPresentationStyle = UIModalPresentationFormSheet; [rootController presentModalViewController:nav animated:YES]; if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) { nav.view.superview.backgroundColor = [UIColor clearColor]; nav.view.bounds = CGRectMake(0, 0, size.width, size.height); } else { nav.view.superview.bounds = CGRectMake(0, 0, size.width, size.height); } }
composeTweetController = [[ComposeTweet alloc] initWithNibName:@"ComposeTweet" bundle:nil]; composeTweetController.modalPresentationStyle = UIModalPresentationFormSheet; [self presentModalViewController:composeTweetController animated:TRUE]; //if you want to change its size but the view will remain centerd on the screen in both portrait and landscape then: composeTweetViewController.view.superview.bounds = CGRectMake(0, 0, 320, 480); //or if you want to change it's position also, then: composeTweetViewController.view.superview.frame = CGRectMake(0, 0, 320, 480);
A partir do iOS7, você pode simplesmente fazer
composeTweetController.preferredContentSize = CGSizeMake(380.0, 550.0);
Testado e funciona para iOS 6, usando o XCode 4.5
Eu me deparei com a minha resposta depois de ler muitas das dicas aqui:
No método viewWillAppear:(BOOL)animated
:
[super viewWillAppear:animated]; //resize modal view self.view.superview.bounds = CGRectMake(0, 0, 432, 680);
No método viewDidAppear:(BOOL)animated
:
CGPoint centerPoint = CGPointMake([[UIScreen mainScreen] bounds].size.width/2, [[UIScreen mainScreen] bounds].size.height/2); self.view.superview.center = centerPoint;
Eu tentei adicionar o código de centralização no mesmo lugar que o código de redimensionamento, mas isso não funcionou. Por alguma razão, só funciona depois que a exibição apareceu na canvas.
Eu suponho que isso tenha algo a ver com a maneira que o UIModalPresentationFormSheet funciona, porque quando eu estava percorrendo o depurador LLDB, notei que havia uma variável _formSheetSize que ainda era {540, 620}. Vai saber.
Isso funcionará com qualquer UIModalTransitionStyle
@interface BaseDialog () @property(nonatomic,assign) CGRect origFrame; @end @implementation BaseDialog -(void)viewDidLoad{ [super viewDidLoad]; self.origFrame=self.view.frame; } -(CGSize)formSheetSize{ return self.origFrame.size; }
A seguir, o primeiro código é como apresentar seu controlador de exibição de modelo
composeTweetController = [[ComposeTweet alloc] initWithNibName:@"ComposeTweet" bundle:nil]; composeTweetController.modalPresentationStyle = UIModalPresentationFormSheet; UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:composeTweetController]; navigationController.delegate = self; [self presentModalViewController:navigationController animated:YES];
E na sua class de controlador ComposeTweet
-(void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; self.navigationController.view.superview.frame = CGRectMake(0, 0,500,400); }
iOS 7
Portanto, a abordagem de definir o quadro ou limites da super visão não funciona mais no iOS 7 (para UIModalTransitionStyleCoverVertical). Você pode, no entanto, agora definir a cor de fundo da superview para limpar e, em seguida, alterar o quadro do controlador de modo de exibição modal como achar melhor.
Então, no iOS 7, você vai querer:
Eu tenho uma visão modal de canvas cheia de UIPresentationFormSheet
com apenas esta linha:
modalViewController.view.superview.bounds = CGRectMake(0, 0, 1024, 748);
@bdrobert infelizmente sua boa solução não funciona mais no iOS6 – o contêiner mantém o tamanho original mesmo que o novo viewcontroller esteja embutido no tamanho personalizado.
Você provavelmente precisará usar a API de contenção introduzida no iOS5 , mas precisará adicionar um plano de fundo esmaecido, buscando todos os events de toque para essa área.
Na mudança de orientação, este código funciona perfeito ….
settingViewController.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentViewController:settingViewController animated:YES completion:nil]; settingViewController.view.superview.frame = CGRectMake(0, 0, 700, 700); UIInterfaceOrientation currentOrientation = [self getDeviceOrientation]; if(currentOrientation == UIInterfaceOrientationPortrait || currentOrientation == UIInterfaceOrientationPortraitUpsideDown) { CGPoint centerPoint = CGPointMake([[UIScreen mainScreen] bounds].size.width/2, [[UIScreen mainScreen] bounds].size.height/2); settingViewController.view.superview.center = centerPoint; } else { CGPoint centerPoint = CGPointMake([[UIScreen mainScreen] bounds].size.height/2, [[UIScreen mainScreen] bounds].size.width/2); settingViewController.view.superview.center = centerPoint; }
O tamanho da vista é fixo. Você terá que implementar as coisas sozinho se quiser algo diferente.