在 Xib 中以编程方式设置背景图像

发布于 2024-12-25 01:22:28 字数 692 浏览 0 评论 0原文

我有一个 XIB 文件,其中包含 UIControlUIScrollView 元素。我想向视图添加背景图像。我尝试在 IB 中添加 ImageView,但无法将其显示为背景,并且它遮挡了控制元素。发送 sendViewBack 消息似乎也没有执行任何操作。

当我以编程方式创建 UIImageView 时,它不会显示。

下面是我尝试的代码:

程序化创建

UIImage *imageBackground = [UIImage imageWithContentsOfFile:@"globalbackground"];

UIImageView *backgroundView = [[UIImageView alloc] initWithImage:imageBackground];

[[self view] addSubview:backgroundView];

[[self view] sendSubviewToBack:backgroundView];

处理 NIB 文件

[[self view] sendSubviewToBack:background];

,其中背景是头文件中声明的 IBOutlet 并连接到 IB 中的 NIB 图像视图。

我这里缺少一个步骤吗?

I have a XIB file with UIControl and UIScrollView elements inside of it. I would like to add a background image to the view. I tried adding an ImageView in IB but I could not get it to be present as a background and it obscured the control elements. Sending a sendViewBack message doesn't seem to do anything either.

When I create a UIImageView programmatically, it doesn't show up.

Below is the code I attempted:

Programmatic Creation

UIImage *imageBackground = [UIImage imageWithContentsOfFile:@"globalbackground"];

UIImageView *backgroundView = [[UIImageView alloc] initWithImage:imageBackground];

[[self view] addSubview:backgroundView];

[[self view] sendSubviewToBack:backgroundView];

Dealing with the NIB file

[[self view] sendSubviewToBack:background];

where background is an IBOutlet declared in the header file and connected to the NIB's image view in IB.

Is there a step I'm missing here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若有似无的小暗淡 2025-01-01 01:22:28

设置框架,不要使用 sendSubviewToBack:。如果您正在使用 UIImageViews,则必须使用 [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"imageName.png"]];

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

希望这就是交易。

Set the frame and dont use sendSubviewToBack:. If you are working with UIImageViews you have to use [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"imageName.png"]];

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

hope this was the deal.

机场等船 2025-01-01 01:22:28
  • 不要将图像视图添加为滚动视图的子视图,它需要是层次结构顶层的单独视图,然后发送到 Z 顺序的后面。
  • 您需要将滚动视图的背景设置为[UIColor clearColor],并确保滚动视图未标记为不透明。您可以在代码或界面生成器中执行此操作。
  • 不要使用imageWithContentsOfFile,然后只向其传递一个不带扩展名的文件名(我假设为.png) - 这可能会返回nil。使用 imageNamed: (在这种情况下,您不提供扩展,iOS4 及更高版本)

根据图像的性质,您还可以用它生成颜色并使用 /em> 作为滚动视图的背景颜色。我假设 self.view 是滚动视图:

self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"globalBackground"]];
  • Don't add the image view as a subview of the scroll view, it needs to be a separate view at the top level of the hierarchy, then sent to the back of the Z-order.
  • You will need to set the background of your scroll view to [UIColor clearColor], and ensure that the scroll view is not marked as opaque. You can do this in code or in interface builder.
  • Don't use imageWithContentsOfFile and then just pass it a filename with no extension (I'm assuming .png) - this is probably returning nil. Use imageNamed: instead (you don't supply an extension in that case, iOS4 and above)

Depending on the nature of your image, you can also generate a colour with it and use that as the background colour of your scroll view. I'm assuming self.view is the scroll view:

self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"globalBackground"]];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文