如何调整UIModalPresentationFormSheet的大小?

She*_*lam 45 iphone cocoa-touch objective-c uikit

我试图将模态视图控制器显示为UIPresentationFormSheet.视图出现,但我似乎无法调整大小.我的XIB具有适当的高度和宽度,但是当我这样调用它时它似乎被覆盖了:

composeTweetController = [[ComposeTweet alloc] initWithNibName:@"ComposeTweet" bundle:nil];
composeTweetController.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentModalViewController:composeTweetController animated:TRUE];
Run Code Online (Sandbox Code Playgroud)

有什么想法吗?我正在使用iPhone SDK 3.2 beta 4

Bro*_*son 91

您可以在呈现后调整模态视图的框架:

在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];
}
Run Code Online (Sandbox Code Playgroud)

  • 它也可以在iPad上使用,但是......如果我旋转设备,视图不会居中 (6认同)
  • @MauroDelrio为了使其在横向上正常工作,您需要翻转中心坐标.例如:CGPoint center = self.view.center; targetController.view.superview.center = {isPortrait}?center:CGPointMake(center.y,center.x); (4认同)
  • 小心这种方法,由于缩放,它会导致视图控制器视图中出现一些失真. (3认同)
  • 需要IOS7的解决方案 (2认同)

psy*_*psy 13

这是一个适用于iOS7以及iOS5和iOS6的方法:(弧形)

-(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);
    }
}
Run Code Online (Sandbox Code Playgroud)


Sku*_*pps 12

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);


小智 9

从iOS7开始,你可以做到

composeTweetController.preferredContentSize = CGSizeMake(380.0, 550.0);
Run Code Online (Sandbox Code Playgroud)