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.
问题:
回答1:
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:
- Create a group in your WatchKit scene. You are likely to want to set custom size attributes for the view (e.g., under Size setting Width and Height as 'Relative to Container' with a value '1' to fill the scene.
- Set your desired background image as the Background attribute for the group.
- Place your label inside the group, and format it as desired.
This group image can also be set programmatically:
- 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"]];
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.
回答2:
I used setBackgroundImageNamed:
and it worked. Just pass the name of the image along with the extension. Make sure that the image is in your WatchKit app bundle or your device-side cache
[self.groupA setBackgroundImageNamed:@"image.png"];