Apple Watch set background image

How can I programmatically set the background image of the WatchKit app? I need to set this in the code, since it changes depending on the size of the variable, and we need to put an inscription on the image.

+8
ios watchkit apple-watch
source share
2 answers

It is not possible to program the background image on the entire Watch App page in WatchKit ... the background image for the entire page can be set at the current time only in the Builder interface.

However, you can set the image as a background behind the label and make this image fill the screen: Set a background image on a Group in WatchKit

  • Create a group in your WatchKit frame. You will probably want to set custom size attributes for the view (for example, in the Size section, set Width and Height to Container Relative with a value of 1 to fill the scene.
  • Set the desired background image as the Background attribute for the group.
  • Place the label inside the group and format it as desired.

This group image can also be set programmatically:

  • Set the IBOutlet for the group to the WatchKit extension in the usual way by dragging Ctrl from the group to the Interface Controller class in Extension.
  • Make sure that your image is included as a “Stock Copy Resource” in the WatchKit app (and not in Extension).
  • Set the background image in the awakeWithContext method of the extension (assuming you called the IBOutlet 'group):

     [self.group setBackgroundImage:[UIImage imageNamed:@"myImageName.png"]]; 

    These two methods, of course, are not mutually independent. You can set the initial image in Interface Builder, and then replace it programmatically later if called.

+21
source share

I used setBackgroundImageNamed: and it worked. Just pass the image name along with the extension. Ensure that the image is in the WatchKit app bundle or cache on the device side.

 [self.groupA setBackgroundImageNamed:@"image.png"]; 
+3
source share

All Articles