Apple Watch set background image

前端 未结 2 557
长发绾君心
长发绾君心 2021-02-15 04:19

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.

2条回答
  •  甜味超标
    2021-02-15 04:50

    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: Set a background image on a Group in WatchKit

    1. 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.
    2. Set your desired background image as the Background attribute for the group.
    3. Place your label inside the group, and format it as desired.

    This group image can also be set programmatically:

    1. 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.
    2. Ensure your image is included as a 'Copy Bundle Resource' in your WatchKit App (not the Extension).
    3. 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.

提交回复
热议问题