[英]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;
storyboard id
提要storyboard id
分配給storyboard id
提要中的abcViewController,例如“ firstView”。 info.plist
,刪除“主故事板文件庫名稱”的值。 實例化情節提要,創建窗口對象,並在應用程序委托的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.