Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find height and width of a WKInterfaceImage

I want to programmatically render a UIImage and then display it on Apple Watch in a WKInterfaceImage, which height and width I set to "Relative to Container" (so that it basically takes up the entire screen space). How can I obtain the WKInterfaceImage's width and and height? As far as I can see, there is no frame-, border- oder layer-property I can access from my extension's code. So what is the proper way to get this information?

Weird enough I have found out that there is a setWidth and setHeight method, but I can't find according getter methods.

At the end of the day I am basically looking for the Watch Kit equivalent to this:

@IBOutlet weak var imageView: UIImageView!
//...
var width = imageView.frame.width
var height = imageView.frame.height
like image 352
Christian Avatar asked Mar 11 '15 06:03

Christian


1 Answers

This is a very common misconception with WatchKit.

You cannot query runtime data in this type of way. The reason is that Apple doesn't want you pinging the Watch every time you need to read a value. They want to optimize battery life as much as possible. Instead, you need to "know" what the width and height of the WKInterfaceImage. You defined it in the Storyboard, so you know exactly what it already is. Xscope can be really helpful in figuring out exactly what the size is. Once you know the size, you need to store that size in your WKInterfaceController so that you have the data at runtime.

class ImageInterfaceController : WKInterfaceController {
    let imageSize38mm = CGSize(width: 100, height: 100)
    let imageSize42mm = CGSize(width: 120, height: 120)
}

Hope that helps shed some light on the fact that you can't query layout and sizing information at runtime the same way you can in an iOS app.

like image 53
cnoon Avatar answered Sep 28 '22 10:09

cnoon