【问题标题】:How can I set my UIView's frame from CGRectMake?如何从 CGRectMake 设置 UIView 的框架?
【发布时间】:2012-10-11 02:45:40
【问题描述】:

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

【问题讨论】:

  • 设置图像视图的框架而不是边界。此外,在可见后获取窗口/视图大小。
  • @lukya,我改变了那个方法调用。接得好。但是我还是有无法使用CGRectMake的问题。
  • 你在哪里添加了第二段代码?您是否尝试在添加图像视图后设置框架?
  • 实际上,看起来第一个建议现在也适用于 CGRectMake - 只是我使用的是边界,而不是框架。感谢您的帮助!
  • 将框架添加到其父视图后,通常会更好地设置框架。

标签: iphone objective-c ios cgrectmake


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-18
    • 2017-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-21
    相关资源
    最近更新 更多