如何调整UIModalPresentationFormSheet的大小?
我想显示一个模式视图控制器作为UIPresentationFormSheet。 该视图出现,但我似乎无法调整它。 我的XIB具有适当的高度和宽度,但是当我这样称呼它时似乎会被覆盖:
composeTweetController = [[ComposeTweet alloc] initWithNibName:@"ComposeTweet" bundle:nil]; composeTweetController.modalPresentationStyle = UIModalPresentationFormSheet; [self presentModalViewController:composeTweetController animated:TRUE];
有什么想法吗? 我正在使用iPhone SDK 3.2 beta 4
展示之后,您可以调整模态视图的框架:
在iOS 5.1 – 7.1testing
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]; }
这里有一个适用于iOS7以及iOS5和iOS6的方法:(arc)
-(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);
经testing,适用于iOS 6,使用XCode 4.5
在阅读了这里的许多提示后,我偶然发现了答案:
-
在
viewWillAppear:(BOOL)animated
方法:[super viewWillAppear:animated]; //resize modal view self.view.superview.bounds = CGRectMake(0, 0, 432, 680);
-
在
viewDidAppear:(BOOL)animated
方法:CGPoint centerPoint = CGPointMake([[UIScreen mainScreen] bounds].size.width/2, [[UIScreen mainScreen] bounds].size.height/2); self.view.superview.center = centerPoint;
我试图添加居中代码在resize的代码相同的地方,但没有奏效。 出于某种原因,只有在视图出现在屏幕上之后才起作用。
我推测它与UIModalPresentationFormSheet的工作方式有关,因为当我在LLDBdebugging器中进行的时候,我注意到有一个variables_formSheetSize仍然是{540,620}。 去搞清楚。
这将与任何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; }
第一个代码是如何呈现你的模型视图控制器
composeTweetController = [[ComposeTweet alloc] initWithNibName:@"ComposeTweet" bundle:nil]; composeTweetController.modalPresentationStyle = UIModalPresentationFormSheet; UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:composeTweetController]; navigationController.delegate = self; [self presentModalViewController:navigationController animated:YES];
并在您的ComposeTweet控制器类
-(void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; self.navigationController.view.superview.frame = CGRectMake(0, 0,500,400); }
从iOS7开始,你可以简单地做
composeTweetController.preferredContentSize = CGSizeMake(380.0, 550.0);
IOS 7
所以设置超级视图框架或界限的方法不再适用于iOS 7(对于UIModalTransitionStyleCoverVertical)。 但是,您现在可以将超视图的背景颜色设置为清除,然后更改模态视图控制器的框架,使其看起来合适。
所以在iOS 7中,您将需要:
- 呈现视图控制器(演示模式:UIModalPresentationFormSheet)
- 设置视图控制器超视图背景颜色清除
- 根据需要更改视图控制器的框架(可能使其更小,然后将其置于超视图中)
我从UIPresentationFormSheet
得到了一个全屏的模态视图,只有这一行:
modalViewController.view.superview.bounds = CGRectMake(0, 0, 1024, 748);
@bdrobert可悲的是你的漂亮的解决scheme不再适用于iOS6 – 容器保持原来的大小,即使新的viewcontrollerembedded自定义大小。
您可能需要使用iOS5中引入的遏制API,但您需要自行添加黯淡的背景,获取该区域的所有触摸事件。
在取向的变化,这个代码工作完美….
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; }
视图大小是固定的。 如果你想要不同的东西,你将不得不自己实现。