How to wrap a custom WKInterfaceImage for SwiftUI

Viewed 325

SwiftUI does a good job of providing WKInterfaceObjectRepresentable However, Is there any way to create a custom view for WKInterfaceImage?

Here is what I tried,

import WatchKit
import SwiftUI
import SpriteKit

struct WatchImage: WKInterfaceObjectRepresentable {
    typealias WKInterfaceObjectType = WKInterfaceImage

    func makeWKInterfaceObject(context: WKInterfaceObjectRepresentableContext<WatchImage>) -> WKInterfaceImage {
        return WKInterfaceImage()
    }

    func updateWKInterfaceObject(_ wkInterfaceObject: WKInterfaceImage, context: Context) {
        wkInterfaceObject.setImage(UIImage(named: "start"))
   }
}

But code is not compiling and giving an error on line return WKInterfaceImage() and says init() is unavailable

This is probably because we can not create an object of WKInterfaceImage programmatically. We need to use it with storyboard.

So Is there any workaround for this?

1 Answers
Related