6

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
4

2 回答 2

7

对于 WatchKit,这是一个非常常见的误解。

您不能以这种方式查询运行时数据。原因是 Apple 不希望您每次需要读取值时都 ping Watch。他们希望尽可能地优化电池寿命。相反,您需要“知道” WKInterfaceImage. 您在 Storyboard 中定义了它,因此您确切地知道它已经是什么。Xscope可以非常有助于确定确切的大小。知道大小后,您需要将该大小存储在您的WKInterfaceController文件中,以便在运行时获得数据。

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

希望这有助于阐明这样一个事实,即您无法像在 iOS 应用程序中那样在运行时查询布局和大小信息。

于 2015-03-11T15:56:53.480 回答
1

可悲的是,您不能以编程方式设置图像的宽度和高度。将图像模式设置为适合内容的大小,并且与容器视图相同。它会自行调整大小以适应内容。

引用类引用中唯一可用的属性 在此处输入图像描述

于 2015-03-11T07:01:30.143 回答