简体   繁体   English

如何从CGRectMake设置UIView的框架?

[英]How can I set my UIView's frame from CGRectMake?

I am new to iOS development, but have had an extensive crash course recently (BNR guid to iOS Programming). 我是iOS开发的新手,但是最近参加了广泛的速成课程(BNR指导iOS编程)。 I am working on modally adding views to an app. 我正在尝试将视图添加到应用程序中。 I currently do not have a View Controller, so my AppDelegate's application:didFinishLaunchingWithOptions method looks as follows: 我目前没有视图控制器,因此我的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;
}

After the view loads, I make a call to add a subview, but I cannot set the frame size using CGRectMake . 视图加载后,我调用添加一个子视图,但是无法使用CGRectMake设置帧大小。 It doesn't show up on screen. 它不会显示在屏幕上。 The only thing I have found to work is using the screen size, but this is not what I want. 我发现唯一起作用的是使用屏幕尺寸,但这不是我想要的。 Here is the code for adding this subview: 这是添加此子视图的代码:

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);

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

I was setting the bounds attribute, when I should have been setting frame . 当我应该设置frame时,我正在设置bounds属性。 Making this change fixed the issue. 进行此更改可解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM