繁体   English   中英

如何从CGRectMake设置UIView的框架?

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

我是iOS开发的新手,但是最近参加了广泛的速成课程(BNR指导iOS编程)。 我正在尝试将视图添加到应用程序中。 我目前没有视图控制器,因此我的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);

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

当我应该设置frame时,我正在设置bounds属性。 进行此更改可解决此问题。

暂无
暂无

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

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