【问题标题】:How to set background image for a View?如何为视图设置背景图像?
【发布时间】:2012-03-08 21:15:14
【问题描述】:

默认背景色是白色,我想把它改成图片

我试过这个:

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

但背景变黑,图像不在背景中。我不知道为什么。

顺便说一句,如果我在一个视图中有超过 1 个图像,如何设置它们的显示优先级?

【问题讨论】:

  • 应该可以,你确定你导入了你的图片吗?
  • 它现在可以工作了,但我真的不知道为什么......我什么都没改变......

标签: iphone objective-c ios view


【解决方案1】:
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"imageone"]];
backgroundView.frame = self.view.bounds;
[[self view] addSubview:backgroundView];

试试这个,这是为框架调用self.view.bounds,而不是为框架调用self.view.frame。我刚刚测试了它,它工作得很好。

编辑:如果您只想将图像发送到堆栈的后面,您可以执行以下操作并选择发送到后面。

【讨论】:

  • 这对我有用。确保你有正确导入的图像。
  • 我从来没有用这种方法在视图上看到过图像……有时是白色的,有时是黑色的……也许其他地方有问题。只有当我在我的 .xib 文件上放置“图像视图”时,图像才会显示出来,但它在前面而不是后面......
  • 看到您是本站新手,请在此选择您认为对您最有帮助的答案,并标记为正确答案。
【解决方案2】:

给你的代码:

    UIImageView *myImage = [[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"background.png"]]autorelease];
myImage.frame = self.view.frame;
[self.view addSubview:myImage];

【讨论】:

  • 我忘记了 [self.view addSubview:myImage];试试 ;)
  • 你真好,但背景保持白色......T.T
【解决方案3】:

你应该使用 UIImageView 并且它的框架是 uiview 的框架。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-11
    • 2010-11-25
    • 2013-10-28
    • 1970-01-01
    • 2011-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多