【问题标题】:Two UIImageViews with same frame but not overlapped exactly具有相同框架但不完全重叠的两个 UIImageView
【发布时间】:2011-11-14 18:05:46
【问题描述】:

第一个 UIImageView 使用框架 (0,38,250,250) 创建,第二个 UIImageView 使用第一个视图的框架创建并添加为第一个图像视图的子视图。结果显示第一个图像视图没有完全被第二个图像视图覆盖。为什么两个视图不重叠?我分配了两个不同背景颜色的图像视图,这样更容易看到。

UIImageView *firstView = [[[UIImageView alloc] initWithFrame:CGRectMake(0,38,250,250)] autorelease];
[firstView setBackgroundColor:RGBACOLOR(36, 39, 39, 0.8)];
[self.view addSubview:firstView];

UIImageView *itemPic = [[[UIImageView alloc] initWithFrame:firstView.frame] autorelease];
itemPic.image = [UIImage imageNamed:@"color_c.png"];
[itemPic setBackgroundColor:RGBACOLOR(150, 150, 35, 0.8)];
[firstView addSubview:itemPic];

【问题讨论】:

    标签: iphone objective-c cocoa-touch uiimageview


    【解决方案1】:

    框架在不同的坐标系中定义。每个视图的框架都在其父视图的坐标系中定义。该坐标系通常将 (0.0, 0.0) 作为父视图的左上角。具有没有原点 (0.0, 0.0) 的框架的子视图将不会与其父视图对齐。

    要始终让子视图与其父视图对齐并填充其父视图,请使用:

    view.frame = parentView.bounds;
    

    【讨论】:

      【解决方案2】:

      如果您想向第一个视图添加第二个视图,则为第二个视图创建框架CGRectMake(0, 0, 250, 250); 在这种情况下,两个视图将具有相同的坐标。

      如果您想向self.view 添加第二个子视图,请使用firstView.frame 创建它。在这种情况下,两个视图将具有相同的坐标。

      【讨论】:

        【解决方案3】:

        子视图被框架的偏移量所抵消,您需要到initWithFrame:firstView.bounds。 Bound 是一个没有偏移量的CGRect(原点为 0,0)。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-11-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多