如何调整UIModalPresentationFormSheet的大小?

时间:2010-03-16 20:39:40

标签: iphone objective-c cocoa-touch uikit

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

composeTweetController = [[ComposeTweet alloc] initWithNibName:@"ComposeTweet" bundle:nil];
composeTweetController.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentModalViewController:composeTweetController animated:TRUE];

有什么想法?我使用的是iPhone SDK 3.2 beta 4

13 个答案:

答案 0 :(得分: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];
}

答案 1 :(得分: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);
    }
}

答案 2 :(得分: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);

答案 3 :(得分:9)

从iOS7开始,你可以简单地做到

composeTweetController.preferredContentSize = CGSizeMake(380.0, 550.0);

答案 4 :(得分:4)

使用XCode 4.5测试并适用于iOS 6

我在阅读了很多关于这里的提示后偶然发现了我的答案:

  1. viewWillAppear:(BOOL)animated方法中:

    [super viewWillAppear:animated];
    
     //resize modal view
     self.view.superview.bounds = CGRectMake(0, 0, 432, 680);
    
  2. viewDidAppear:(BOOL)animated方法中:

    CGPoint centerPoint = CGPointMake([[UIScreen mainScreen] bounds].size.width/2, [[UIScreen mainScreen] bounds].size.height/2);
    self.view.superview.center = centerPoint;
    
  3. 我尝试将中心代码添加到与调整大小代码相同的位置,但这不起作用。出于某种原因,它仅在视图出现在屏幕上后才有效。

    我猜测它与UIModalPresentationFormSheet的工作方式有关,因为当我在LLDB调试器中单步执行时,我注意到有一个变量_formSheetSize仍然是{540,620}。去图。

答案 5 :(得分:4)

这适用于任何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;
}

答案 6 :(得分:4)

以下代码是如何呈现模型视图控制器

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

答案 7 :(得分:3)

iOS 7

因此,设置superview的框架或边界的方法在iOS 7上不再起作用(对于UIModalTransitionStyleCoverVertical)。但是,您现在可以将超视图的背景颜色设置为清除,然后根据您的需要更改模态视图控制器的框架。

所以在iOS 7中你需要:

  1. 呈现视图控制器(演示模式:UIModalPresentationFormSheet)
  2. 设置视图控制器superview背景颜色以清除
  3. 根据需要更改视图控制器的框架(可能使其更小,然后将其置于超视图中)

答案 8 :(得分:2)

我从UIPresentationFormSheet获得了一个全屏模式视图,只有这一行:

modalViewController.view.superview.bounds = CGRectMake(0, 0, 1024, 748);

答案 9 :(得分:2)

@bdrobert遗憾的是,你的漂亮解决方案在 iOS6 上不再有效 - 即使新的viewcontroller嵌入了自定义大小,容器也保持原始大小。

您可能需要使用 iOS5 中引入的包含API,但您需要自己添加灰色背景,获取该区域的所有触摸事件。

答案 10 :(得分:0)

在改变方向时,这段代码完美无缺......

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

答案 11 :(得分:0)

现代ios13方式:

使用弹出窗口(表单和页面忽略preferredContentSize)

然后设置或覆盖import jaydebeapi, sys #Enter the values for you database connection dsn_database = "testdb"# e.g. "BLUDB" Name of the database dsn_hostname = "localhost"# e.g.: "bluemix05.bluforcloud.com" dsn_port = "50000"# e.g. "50000" Database port number dsn_uid = "DB2INST1"# e.g. "dash104434" User id dsn_pwd = "123456789"# e.g. "7dBZ3jWt9xN6$o0JiX!m" User password for the database connection_string='jdbc:db2://'+dsn_hostname+':'+dsn_port+'/'+dsn_database if (sys.version_info >= (3,0)): conn = jaydebeapi.connect("com.ibm.db2.jcc.DB2Driver", connection_string, [dsn_uid, dsn_pwd]) else: conn = jaydebeapi.connect("com.ibm.db2.jcc.DB2Driver", [connection_string, dsn_uid, dsn_pwd]) [1]: https://www.ibm.com/support/knowledgecenter/en/SSGNPV_1.1.3/dsx/createconnectionsdirect.html ,又名Ed Pilowat&Co

答案 12 :(得分:-4)

视图大小是固定的。如果你想要不同的东西,你必须自己实施。