簡體   English   中英

將UIViewcontroller的視圖添加到UIWindow不會顯示

[英]adding UIViewcontroller's view to UIWindow does not show

我正在嘗試使用以下故事板查看帶有以下代碼的視圖,但是未制作uiviewcontroller的場景初始視圖控制器。 這是我在AppDelegate的didFinishLaunchingWithOption中編寫的代碼。

UIWindow* window = [UIApplication sharedApplication].keyWindow;  
abcViewController *controller = [[abcViewController alloc]init];  
UIView *redView = [[UIView alloc] initWithFrame: CGRectMake ( [[UIScreen mainScreen] bounds].origin.x+30, [[UIScreen mainScreen] bounds].origin.y+30, 260, 400)];
[redView setBackgroundColor:[UIColor redColor]];
UIView *greenView = [[UIView alloc] initWithFrame: CGRectMake ( redView.frame.origin.x + 10.0f, redView.frame.origin.y + 10.0f, 180, 320)];
[greenView setBackgroundColor:[UIColor greenColor]];
[redView addSubview:greenView];
[controller.view addSubview:redView];
window.rootViewController = controller;
[window makeKeyAndVisible];
return YES;
  1. 首先將storyboard id提要storyboard id分配給storyboard id提要中的abcViewController,例如“ firstView”。
  2. 在應用程序委托中導入viewController
  3. 在情節提要中,取消選中第一個視圖控制器中的“是初始視圖控制器”屬性。
  4. 在應用程序的info.plist ,刪除“主故事板文件庫名稱”的值。
  5. 實例化情節提要,創建窗口對象,並在應用程序委托的application:didFinishLaunchingWithOptions:設置初始視圖控制器application:didFinishLaunchingWithOptions:方法

     - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds]; UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; TestViewController *controller = [storyboard instantiateViewControllerWithIdentifier:@"firstView"]; UIView *redView = [[UIView alloc] initWithFrame: CGRectMake ( [[UIScreen mainScreen] bounds].origin.x+30, [[UIScreen mainScreen] bounds].origin.y+30, 260, 400)]; [redView setBackgroundColor:[UIColor redColor]]; UIView *greenView = [[UIView alloc] initWithFrame: CGRectMake ( redView.frame.origin.x + 10.0f, redView.frame.origin.y + 10.0f, 180, 320)]; [greenView setBackgroundColor:[UIColor greenColor]]; [redView addSubview:greenView]; [controller.view addSubview:redView]; self.window.rootViewController = controller; [self.window makeKeyAndVisible]; return YES; } 

暫無
暫無

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

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