将原始自定义视图添加到窗口

时间:2013-12-01 06:26:22

标签: ios iphone objective-c xcode uiview

我将自定义视图添加到窗口但不起作用。我试图解决它,但它不能很好地工作。 (我使用空项目模板创建了这个项目而不使用故事板)。此屏幕应显示屏幕右下角的红色矩形。

我想将显示红色屏幕的自定义视图添加到窗口,但它只显示白屏。

AppDelegate.m:

//
//  HypnosisterAppDelegate.m
//  Hypnosister
//
//  Created by TSH on 12/1/13.
//  Copyright (c) 2013 TSH. All rights reserved.
//

#import "HypnosisterAppDelegate.h"
#import "HypnosisterViewController.h"
#import "HypnosisView.h"

@implementation HypnosisterAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    HypnosisterViewController *test = [[HypnosisterViewController alloc]     initWithNibName:@"HypnosisterViewController" bundle:nil];
    UINavigationController *nav = [[UINavigationController alloc]  initWithRootViewController:test];
    self.window.rootViewController = nav;

    CGRect viewFrame = CGRectMake(16, 24, 10, 15);

    HypnosisView *view = [[HypnosisView alloc] initWithFrame:viewFrame];
    [view setBackgroundColor:[UIColor redColor]];
    [[self window] addSubview:view];

    self.window.backgroundColor = [UIColor whiteColor];

    [self.window makeKeyAndVisible];
    return YES;
}

@end

[编辑]

我搞砸了订单。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

    HypnosisterViewController *test = [[HypnosisterViewController alloc]     initWithNibName:@"HypnosisterViewController" bundle:nil];
    UINavigationController *nav = [[UINavigationController alloc]  initWithRootViewController:test];
    self.window.rootViewController = nav;

    CGRect viewFrame = CGRectMake(160, 240, 100, 150);

    HypnosisView *view = [[HypnosisView alloc] initWithFrame:viewFrame];
    [view setBackgroundColor:[UIColor redColor]];
    [[self window] addSubview:view];

    return YES;
}

3 个答案:

答案 0 :(得分:1)

当你将navigationcontroller设置为rootviewcontroller时,每次出现HypnosisterViewController都会毫无疑问。如果要添加自定义视图,必须在HypnosisterViewController上添加为子视图。

 HypnosisView *view = [[HypnosisView alloc] initWithFrame:viewFrame];
[view setBackgroundColor:[UIColor redColor]];
[test.view addSubview:view];

希望它会起作用

答案 1 :(得分:0)

有两点

1- iOS中视图的坐标从左上角开始,因此此框架(16,24,10,15)将位于视图的左上角,框架计算为(x,y,宽度,高度) )

2- Window不响应设备方向。它将永远是肖像。如果您旋转设备,则需要重新计算视图坐标。

答案 2 :(得分:0)

@toshi您的代码是正确的,只是您错过了订购。使用此代码它将工作。让我知道它是否工作。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    HypnosisterViewController *test = [[HypnosisterViewController alloc]     initWithNibName:@"HypnosisterViewController" bundle:nil];
    UINavigationController *nav = [[UINavigationController alloc]  initWithRootViewController:test];
    self.window.rootViewController = nav;
    [self.window makeKeyAndVisible];


    CGRect viewFrame = CGRectMake(16, self.window.frame.size.height - 24, 10, 15);

    HypnosisView *view = [[HypnosisView alloc] initWithFrame:viewFrame];
    [view setBackgroundColor:[UIColor redColor]];
    [[self window] addSubview:view];

    self.window.backgroundColor = [UIColor whiteColor];

    return YES;
}