单视图应用程序具有黑色背景,无法更改

时间:2018-07-09 09:36:27

标签: ios objective-c

我正在使用objective-c开发一个针对11.4 iOS的单视图应用程序,而Xcode的版本是9.4.1。

创建后,有Main.storyboard和LaunchScreen.storyboard,我将Main.storyboard的背景更改为黄色。

然后使用打击代码片段加载rootViewController

self.window.rootViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateInitialViewController];

当我运行该应用程序时,该应用程序的背景是黑色而不是黄色,这是怎么回事?

======================== 添加屏幕截图: enter image description here

======================== 添加用于初始化rootviewcontroller的代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen alloc] bounds]];

    self.window.rootViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"ViewController"];

    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

======================= 我可以确认这是didFinishLaunchingWithOptions中的代码失败了,如果我在方法中注释掉那些代码,一切都很好,不知道为什么。

5 个答案:

答案 0 :(得分:2)

您无需启动任何代码即可获得Main.storyboard初始视图控制器。

删除代码,然后转到Project settings -> General tab,在这里您会发现Main Interface,看看它是否设置为Main(您的情节提要名称)

enter image description here

还要在您的storyboard中检查您的进入点是否已设置为所需的viewcontroller

enter image description here

运行项目并查看。

答案 1 :(得分:1)

使用以下代码启动视图控制器,

self.window.rootViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"yourViewController Storyboard ID."];

答案 2 :(得分:1)

找到了原因

initWithFrame:[[UIScreen alloc] bounds] 

应该是

initWithFrame:[[UIScreen mainScreen] bounds]

答案 3 :(得分:1)

使用此代码。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    self.window.rootViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"ViewController"];

    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

答案 4 :(得分:0)

您已经在项目中添加了“ Main.storyboard”。 选择其ViewController,转到属性检查器。勾选“初始视图控制器”选项。 在“ Main.storyboard”中创建View控制器,例如LoginVC,为其提供Class和Storyboard ID

在应用程序委托中创建rootviewcontroller。在此处输入图片描述 enter image description here

enter image description here

let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let initialViewControlleripad : UIViewController = mainStoryboardIpad.instantiateViewController(withIdentifier: "LoginVC") as UIViewController
        self.window = UIWindow(frame: UIScreen.main.bounds)
        self.window?.rootViewController = initialViewControlleripad
        self.window?.makeKeyAndVisible()