【问题标题】:Add image to view (background)将图像添加到视图(背景)
【发布时间】:2015-08-14 17:22:43
【问题描述】:

我已在代码中将图像添加到我的视图控制器。不使用图像视图。我就是这样做的:

UIImageView *backgroundImage =[[UIImageView alloc] initWithFrame:CGRectMake(50,50,100,500)];
backgroundImage.image=[UIImage imageNamed:@"backgroundImage"];
[self.view addSubview:backgroundImage];

有两件事我需要一点帮助。我怎样才能将图像放在视图的后面、标签、按钮等后面。 以及如何让图像适合所有 iphone 屏幕尺寸? 我希望你能帮助我。 非常感谢

【问题讨论】:

  • [self.view insertSubview:backgroundImage atIndex:0];?
  • 尝试使用 insertSubview: backgroundImage atIndex:0 将图像放在所有其他视图的后面。
  • 只需使用colorWithPatternImage属性设置颜色 self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"backgroundImage"]];
  • @Larme 成功了。非常感谢!
  • @guardabrazo 成功了。欣赏!

标签: ios objective-c iphone


【解决方案1】:
UIImageView *backgroundImage =[[UIImageView alloc]  initWithFrame:self.view.bounds];  // This will set image fit to all iphone...

backgroundImage.image=[UIImage imageNamed:@"your_image.png"];
[self.view addSubview:backgroundImage];

// 此行将在所有其他控件后面发送图像

[backgroundImage sendSubviewToBack:self.view];

【讨论】:

    【解决方案2】:

    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"backgroundImage"]];

    【讨论】:

      【解决方案3】:

      正如你问了两个问题-

      问题 1如何让其他子视图的标签/按钮在图像上可见?

      回答 -

      只需在任何其他子视图之前添加图像。

      还有两种方法可以将背景图片设置为UIView——

      您可以将view 背景颜色设置为使用UIColor’s colorWithPaternImage 创建的颜色。 您可以将UIImageView subview 添加到您的view

      colorWithPaternImage 被创建为使用小图像来创建将重复的图案图像。将它与大图像一起使用不是一个明智的决定。因此,如果您想要一个带有图案的图像背景,并使用将重复填充背景的小图像,那么您应该使用colorWithPaternImage,因为它会很快完成并且不会消耗太多内存。

      self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background"]];
      

      如果您有完整尺寸的背景图片,那么您绝对应该使用UIImageView。在这种情况下使用UIImageView 会节省大量内存。

      UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background"]];
      [self.view addSubview:backgroundView];
      

      问题 2如何根据设备/屏幕分辨率显示图像?

      回答 -

      CGRect screenBounds = [[UIScreen mainScreen] bounds]; 
      

      使用上面的screenBounds设置ImageView的框架。

      【讨论】:

      • 很好的解释。
      【解决方案4】:

      只需用以下代码替换您的代码:

          UIImageView *backgroundImage = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
          backgroundImage.image = [UIImage imageNamed:@"backgroundImage"];
          [self.view insertView:backgroundImage atIndex:0];
      

      希望这会有所帮助。让我知道.. :)

      【讨论】:

        【解决方案5】:

        通过以下方法简单获取设备的屏幕尺寸,使其兼容所有设备。

        CGRect rect = [UIScreen mainScreen].bounds;
        

        创建该帧大小的 UIImageView 对象并插入到 UIView 的索引 0。

        UIImageView *backgroundImage =[[UIImageView alloc]  initWithFrame:rect];  // This will set image fit to all iphone...
        
        backgroundImage.image=[UIImage imageNamed:@"your_image.png"];
        [self.view insertSubview:backgroundImage atIndex:0];
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-12-23
          • 1970-01-01
          • 1970-01-01
          • 2013-02-16
          相关资源
          最近更新 更多