在 Xib 中以编程方式设置背景图像
我有一个 XIB 文件,其中包含 UIControl
和 UIScrollView
元素。我想向视图添加背景图像。我尝试在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
设置框架,不要使用
sendSubviewToBack:
。如果您正在使用 UIImageViews,则必须使用[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"imageName.png"]];
希望这就是交易。
Set the frame and dont use
sendSubviewToBack:
. If you are working with UIImageViews you have to use[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"imageName.png"]];
hope this was the deal.
[UIColor clearColor]
,并确保滚动视图未标记为不透明。您可以在代码或界面生成器中执行此操作。imageWithContentsOfFile
,然后只向其传递一个不带扩展名的文件名(我假设为.png) - 这可能会返回nil
。使用imageNamed:
(在这种情况下,您不提供扩展,iOS4 及更高版本)根据图像的性质,您还可以用它生成颜色并使用 /em> 作为滚动视图的背景颜色。我假设 self.view 是滚动视图:
[UIColor clearColor]
, and ensure that the scroll view is not marked as opaque. You can do this in code or in interface builder.imageWithContentsOfFile
and then just pass it a filename with no extension (I'm assuming .png) - this is probably returningnil
. UseimageNamed:
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: