3

我已經通過搜索看到了一些這些問題,但主要與故事板有關。模態視圖控制器加載黑色背景

我只是在實用上創建一個模態視圖控制器。它實際上是與可達性的使用中,一旦連接被視爲NotReachable我提出一個模式視圖控制器是這樣的:

-(void)checkConnection: (Reachability*) curReach { 
    NetworkStatus netStatus = [curReach currentReachabilityStatus]; 
     if (netStatus == NotReachable) { 
      NSLog(@"inernet reach - not reachable"); 

      UIViewController *modalViewController = [[MESConnectionModalViewController alloc] init]; 
      modalViewController.view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.2f]; 
      modalViewController.view.opaque= YES; 
      [self.window.rootViewController presentModalViewController:modalViewController animated:YES]; 

} 
} 

在視圖控制器MESConnectionModalViewController目前還沒有代碼,只是標準。

當模態視圖轉換到當前視圖時,背景看起來是正確的(這是短暫的一兩秒鐘)。一旦模態完全在屏幕上,它就是全黑的,而不是部分黑色。我期望基本上略微覆蓋當前的內容。上面的代碼在應用程序委託中seutp,並在可達性更新時調用,因此我試圖在互聯網連接正在解析時顯示模式視圖控制器。

+0

試圖設置模式視圖比視圖本身較小?如果是這樣,爲什麼不使用cg rect併爲其設置座標,因此當它被呈現時它將比視圖更小。 – 2013-03-17 22:35:16

回答

5

模態視圖不支持透明度(對於iPhone)。
但您可以添加「的UIView」到父視圖並使用CoreAnimation

編輯

-(void)checkConnection: (Reachability*) curReach { 
    NetworkStatus netStatus = [curReach currentReachabilityStatus]; 
    if (netStatus == NotReachable) { 
     NSLog(@"internet reach - not reachable"); 
     UIViewController *modalViewController = [[MESConnectionModalViewController alloc] init]; 
     //Set y position to animate it 
     CGRect frame = modalViewController.view.frame; 
     frame.origin.y = [[UIApplication sharedApplication] keyWindow].frame.size.height; 
     modalViewController.view.frame = frame; 
     modalViewController.view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.2f]; 
     [self.view addSubview:modalViewController.view]; 
     //Animate appearing 
     frame.origin.y = 0; 
     [UIView animateWithDuration:0.2 animations:^{ 
      modalViewController.view.frame = frame; 
     }]; 
    } 
} 

您還可以存儲modalViewController作爲財產的存取權限它在未來製作動畫。

+0

你能解釋一下怎麼做嗎?作爲一個更多的背景,它被用作在整個應用程序中的任何地方向用戶發送消息,所以當連接到互聯網丟失時,在應用程序委託中進行設置。 – StuartM 2013-03-18 07:56:12

+0

感謝您的編輯,我得到兩個錯誤。 [self.view line ...屬性視圖在應用程序委託上找不到。這是因爲我無法將視圖添加到應用程序委託中,我需要將其添加到呈現的視圖控制器(以當前顯示的那個爲準),但我不確定如何執行此操作?其次,在frame.y行上的錯誤...在struct CGRect上沒有名爲y的成員? – StuartM 2013-03-18 17:28:36

+0

編輯第二期。您可以嘗試將子視圖添加到「UIWindow」。 [[[UIApplication sharedApplication] keyWindow] addSubview:modalViewController.view]; – 2013-03-18 17:48:24

1

你需要的是第二個UIWindowAppDelegate類 你可以在所有東西上做一些像UIAlertView! 相信我,這是你需要什麼,並確保設置

myWindow2.windowLevel = UIWindowLevelAlert; 

訪問你的窗口(得到的指針):

UIWindow *myWindow2 = [(AppDelegate *)[UIApplication sharedApplication].delegate myWindow2]; 
+1

你能否提供更多關於需要完成什麼的信息?我應該繼承UIAlertView並更改viewDidLoad以包含視圖的顏色方案嗎?那麼這是如何呈現的? – StuartM 2013-03-18 07:57:10

+0

用於提醒您的選項是iOS默認警報(UIAlertView或UIActionSheet)或創建您自己的:1 - 向AppDelegate添加一個窗口並將其級別設置爲警報級別。 2-添加自定義警報,這是一個UIView基類,它有一個標題標籤,文本標籤和一些按鈕,然後使用window.hidden = [YES/NO]來顯示警報! – 2013-03-18 17:22:12