简体   繁体   English

查找WKInterfaceImage的高度和宽度

[英]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). 我想以编程方式呈现UIImage然后在Apple Watch中以WKInterfaceImage显示它,我将其设置为“相对于容器”的高度和宽度(这样它基本上占用了整个屏幕空间)。 How can I obtain the WKInterfaceImage 's width and and height? 如何获得WKInterfaceImage的宽度和高度? As far as I can see, there is no frame -, border - oder layer -property I can access from my extension's code. 据我所见,没有frame - , border - order layer -property我可以从我的扩展程序代码访问。 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. 很奇怪我发现有一个setWidth和setHeight方法,但我找不到根据getter方法。

At the end of the day I am basically looking for the Watch Kit equivalent to this: 在一天结束时,我基本上寻找与此相当的Watch Kit:

@IBOutlet weak var imageView: UIImageView!
//...
var width = imageView.frame.width
var height = imageView.frame.height

This is a very common misconception with WatchKit. 这是与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. 原因是Apple每次需要读取值时都不希望你ping Watch。 They want to optimize battery life as much as possible. 他们希望尽可能地优化电池寿命。 Instead, you need to "know" what the width and height of the WKInterfaceImage . 相反,您需要“知道” 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. Xscope在确定尺寸的确切方面非常有用。 Once you know the size, you need to store that size in your WKInterfaceController so that you have the data at runtime. 一旦知道了大小,就需要在WKInterfaceController存储该大小,以便在运行时获得数据。

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. 希望这有助于说明您无法在运行时查询布局和调整大小信息,就像在iOS应用程序中一样。

Sadly, you just can't set the width and hight of a an image programmatically. 遗憾的是,您无法以编程方式设置图像的宽度和高度。 Set the image mode to size to fit content and also the same to the container view. 将图像模式设置为适合内容的大小,也与容器视图相同。 It will resize itself to fit the content. 它将调整自身大小以适应内容。

Quoting the only available attributes from the class reference 引用类引用中唯一可用的属性 在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM