簡體   English   中英

為什么我的模態呈現(表單)navController的rootViewController不能以模態呈現時意識到它的尺寸較小?

[英]Why isn't the rootViewController of my modally presented (form sheet) navController aware of it's smaller size when presented modally?

我正在使用具有幾個不同模式視圖的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。 該按鈕幾乎不顯示在屏幕上,因為框架沒有改變。 它仍然顯示為全屏。 這是該項目鏈接

這是iOS7的新功能。 如果將UIViewController嵌入導航欄中,則它不會變小,因為默認情況下導航欄是半透明的。

如果更改視圖控制器的背景顏色,則將看到它的頂部,實際上它的頂部實際上在導航欄的后面。

要在導航欄下方布置v1視圖控制器,可以使用以下代碼:

if ([v1 respondsToSelector:@selector(edgesForExtendedLayout)]) {
    v1.edgesForExtendedLayout = UIRectEdgeNone;
}

它的行為就像在iOS6中一樣。

不確定為什么遇到問題。 我正在使用以下代碼:

- (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中可以為您提供正確的框架,因此,正如我在最初的編輯中所說,您應該可以正確地訪問模式的視圖。

從子視圖控制器(一個不完整的屏幕並且是另一個視圖控制器的子視圖的子視圖控制器)以模態形式顯示視圖控制器時,執行此操作很重要,這樣模態控制器才能知道其出現在畫布中的大小

childViewController.definesPresentationContext = YES; 
modalViewControllerWhichIsAboutToBePushed.modalPresentationStyle = UIModalPresentationCurrentContext

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM