我试图在我使用情节提要的地方查看带有以下代码的视图,但是未制作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分配给故事板上的abcViewController,例如“ firstView”。
在应用程序委托中导入viewController
在情节提要中,取消选中第一个视图控制器中的“是初始视图控制器”属性。
在应用程序的info.plist中,删除“主故事板文件基本名称”的值。
实例化故事板,创建窗口对象,并在应用程序委托的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;
}

关于ios - 将UIViewcontroller的 View 添加到UIWindow不会显示,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19739971/

10-10 14:21