0

我是 iOS 开发的新手,但最近参加了广泛的速成课程(BNR 指导 iOS 编程)。我正在以模态方式向应用程序添加视图。我目前没有 View Controller,所以我的 AppDelegate 的application:didFinishLaunchingWithOptions方法如下所示:

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

    MyUIView *map = [[MapSurface alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    //this one does not work: MyUIView *map = [[MapSurface alloc] initWithFrame:CGRectMake(0, 0, 256, 256)];
    [map setBackgroundColor:[UIColor grayColor]];
    [[self window] addSubview:map];

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

视图加载后,我调用添加子视图,但无法使用CGRectMake. 它不会出现在屏幕上。我发现唯一有用的是使用屏幕尺寸,但这不是我想要的。这是添加此子视图的代码:

UIImage *image = [[UIImage alloc] initWithCGImage:img];//img is a CGImageRef
UIImageView *view = [[UIImageView alloc] initWithImage:image];
CGRect b = CGRectMake(0, 0, 256, 256);
//[view setFrame:b];//nothing shows on screen
//[view setBounds:b];//nothing shows on screen
//[view setBounds:[[UIScreen mainScreen] bounds]];//shows the tan area in the graphic below
[view setFrame:[[UIScreen mainScreen] bounds]];//fills the screen
[map addSubView:view];
CGImageRelease(img);

棕褐色区域甚至没有填满屏幕!

4

1 回答 1

0

我正在设置bounds属性,而我应该设置frame. 进行此更改解决了该问题。

于 2012-11-13T15:22:36.690 回答