我正在使用具有几个不同模式视图的iPad应用程序,此代码非常常见:

UIViewController *v1 = [[UIViewController alloc] init];

UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:v1];
nav1.modalPresentationStyle = UIModalPresentationFormSheet;

[self presentViewController:nav1 animated:YES completion:nil];


可能是我做错了,但这是我模态呈现navController-nested vc的方式。

问题在于,在v1类中,对self.frame / bounds的任何引用都会导致全屏尺寸:768x1024。即使navController显然没有以该大小显示。

我应该怎么做才能使v1 vc知道实际上有多大?这样,如果我想添加一个tableView,它将知道它应该有多大?

谢谢!

编辑:

我已经尝试了其他一些方法,但是仍然没有解决该问题的方法。我做了一个简单的示例项目来说明我遇到的问题。我只有一种观点,这是代码的核心:

- (void)viewDidLoad
{
[super viewDidLoad];

NSLog(@"Frame: %@", NSStringFromCGRect(self.view.frame));
NSLog(@"Bounds: %@", NSStringFromCGRect(self.view.bounds));

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(self.view.frame.size.width - 400, 0, 400, 400);
button.backgroundColor = [UIColor redColor];
[button addTarget:self action:@selector(presentModal) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:button];
}

- (void)presentModal {
SSViewController *view = [[SSViewController alloc] init];

UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:view];
nav.modalPresentationStyle = UIModalPresentationFormSheet;

[self.navigationController presentViewController:nav animated:YES completion:nil];
}


加载该视图时,我有一个大红色按钮位于视图的右上角。当我按下按钮时,它将在navController中嵌入的模式视图中加载相同的VC。该按钮几乎在屏幕外显示,因为框架没有改变。它仍然显示为全屏。这是link to the project

最佳答案

不确定为什么遇到问题。我正在使用以下代码:

- (void)presentNewView {
    NewViewController *newVC = [[NewViewController alloc] initWithNibName:nil bundle:nil];
    newVC.view.backgroundColor = [UIColor redColor];

    UINavigationController *newNC = [[UINavigationController alloc] initWithRootViewController:newVC];
    newNC.modalPresentationStyle = UIModalPresentationFormSheet;

    [self.navigationController presentViewController:newNC animated:YES completion:NULL];
}


..它会在模拟器中产生以下结果:



..当我打印出第一个ViewController的边框和界限(我认为这可能是两者的问题)时,我得到以下信息:

镜框高度:1024.000000
框架宽度:768.000000
界限高度:1024.000000
界限宽度:768.000000

..当我打印出呈现的ViewController的框架/边界时,我得到以下信息:

镜框高度:620.000000
框架宽度:540.000000
界限高度:620.000000
界限宽度:540.000000

您如何确定框架的大小?像我上面显示的那样,以模态形式呈现的v1类中的任何引用都应该知道其实际大小。

编辑

我与您的代码发现的主要区别是,在我的代码中,我创建了视图控制器“ NewViewController”的子类,并从该类中打印出框架。类本身似乎知道其正确的界限,但是所提供的类似乎没有意识到。通过从显示它的ViewController类中打印view属性来证明这一点:

NewViewController的呈现类视图:frame =(0 0; 768 1024)

与从NewViewController本身的ViewDidAppear方法内部打印出self.view相比:

出现了NewViewController的视图:frame =(0 0; 540 576)

故事的寓意是,如果您要以显示的方式呈现UIViewController,则无论如何都可能想要继承UIViewController,以便可以根据需要自定义它,因此在该文件中,如果您引用self .view或self.bounds,您将获得ACTUAL视图/界限。

编辑#2

根据您提供的项目,出现该问题的原因是因为您要打印出viewDidLoad中视图的框架/边界,而不是viewDid / viewWillAppear。将那些NSLog语句添加到VWA或VDA中可以为您提供正确的框架,因此,正如我在最初的编辑中所说的那样,此时您应该可以正确访问模式的视图。

10-07 19:44
查看更多