Apple Watch设置背景图像 [英] Apple Watch set background image
问题描述
如何以编程方式设置我的WatchKit App背景图像?我需要在代码中设置它,因为它根据变量大小而变化,我们需要在图像上放置一个标签。
How can I programmatically set my WatchKit App background image? I need to set this in code as it changes depending on variable size and we need to place a label over the image.
推荐答案
以编程方式无法在WatchKit中的整个Watch App页面上设置背景图像...整个页面的背景图像只能在当前时间设置它出现在Interface Builder中。
It is not possible to programatically set the background image on an entire Watch App page in WatchKit... a background image for the whole page can at the current time only be set in Interface Builder it appears.
但是,可以将图片设置为标签后面的背景,并使该图像填满屏幕:
However, you can set a picture as a background behind a label, and make that image fill the screen:
- 在WatchKit场景中创建一个组。您可能想要为视图设置自定义尺寸属性(例如,在尺寸设置宽度和高度下,相对于容器,值为1以填充场景。
- 将所需的背景图像设置为组的背景属性。
- 将标签放在组中,并根据需要对其进行格式化。
此组图像也可以通过编程方式设置:
This group image can also be set programmatically:
- 将组的IBOutlet设置为WatchKit按照常规方式进行扩展,方法是从组中按Ctrl键拖动到扩展中的Interface Controller类。
- 确保您的图像作为复制包资源包含在WatchKit应用程序中(不是扩展名。。
-
在扩展的
awakeWithContext
方法中设置背景图像(假设你调用了你的IBOutlet' group'):
- Set an IBOutlet for the group to your WatchKit Extension in the usual fashion, by Ctrl-dragging from the group to your Interface Controller class in the Extension.
- Ensure your image is included as a 'Copy Bundle Resource' in your WatchKit App (not the Extension).
Set the background image in the
awakeWithContext
method of the Extension (assuming you called your IBOutlet 'group'):
[self.group setBackgroundImage:[UIImage imageNamed:@"myImageName.png"]];
这两种技术当然不是相互独立的。您可以在Interface Builder中设置初始图像,然后在需要时以编程方式替换它。
The two techniques are of course not mutually independent. You can set an initial image in Interface Builder, and then replace it programatically later if that is called for.
这篇关于Apple Watch设置背景图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!