Ios 如何以编程方式创建UIView,使其不与状态栏重叠

Ios 如何以编程方式创建UIView,使其不与状态栏重叠,ios,uiview,uiviewcontroller,Ios,Uiview,Uiviewcontroller,我正在应用程序委托中使用以下代码以编程方式创建应用程序的窗口和根控制器: - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. self.window = [[UIWindow alloc]

我正在应用程序委托中使用以下代码以编程方式创建应用程序的窗口和根控制器:

- (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 = [[UIViewController alloc] init];
    self.window.rootViewController.view = [[UIView alloc] init];
    self.window.rootViewController.view.backgroundColor = [UIColor redColor];
    return TRUE;
}
代码运行良好,但是与根视图控制器关联的视图与状态栏重叠,我希望避免这种情况(参见下图)

我应该如何进行?特别是我希望状态栏有一个浅灰色的背景(类似于用故事板创建的应用程序),并且正确处理方向的变化


谢谢

好吧,你可以做如下事情:

- (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 = [[UIViewController alloc] init];
    self.window.rootViewController.view.backgroundColor = [UIColor whiteColor];
    UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(0,
                                                               [UIApplication sharedApplication].statusBarFrame.size.height,
                                                               [UIScreen mainScreen].bounds.size.width,
                                                               [UIScreen mainScreen].bounds.size.height)];
    redView.backgroundColor = [UIColor redColor];
    [self.window.rootViewController.view addSubview:redView];

    [self.window makeKeyAndVisible];

    return TRUE;
}
应该是这样的:


好吧,你可以做如下事情:

- (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 = [[UIViewController alloc] init];
    self.window.rootViewController.view.backgroundColor = [UIColor whiteColor];
    UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(0,
                                                               [UIApplication sharedApplication].statusBarFrame.size.height,
                                                               [UIScreen mainScreen].bounds.size.width,
                                                               [UIScreen mainScreen].bounds.size.height)];
    redView.backgroundColor = [UIColor redColor];
    [self.window.rootViewController.view addSubview:redView];

    [self.window makeKeyAndVisible];

    return TRUE;
}
应该是这样的: