Chr*_*ian 4 ios swift watchkit wkinterfaceimage
我想以编程方式渲染a UIImage然后在Apple Watch中显示它WKInterfaceImage,我将其设置为"相对于容器"的高度和宽度(这样它基本上占用了整个屏幕空间).我怎样才能获得WKInterfaceImage宽度和高度?据我所知,没有frame- ,border- oder layer-property我可以从我的扩展程序代码访问.那么获取这些信息的正确方法是什么?
很奇怪我发现有一个setWidth和setHeight方法,但我找不到根据getter方法.
在一天结束时,我基本上寻找与此相当的Watch Kit:
@IBOutlet weak var imageView: UIImageView!
//...
var width = imageView.frame.width
var height = imageView.frame.height
Run Code Online (Sandbox Code Playgroud)
这是与WatchKit非常常见的误解.
您无法以这种方式查询运行时数据.原因是Apple每次需要读取值时都不希望你ping Watch.他们希望尽可能地优化电池寿命.相反,你需要"知道"的宽度和高度WKInterfaceImage.您在故事板中定义了它,因此您确切地知道它已经是什么.Xscope在确定尺寸的确切方面非常有用.一旦知道了大小,就需要将该大小存储在您的大小中,WKInterfaceController以便在运行时获得数据.
class ImageInterfaceController : WKInterfaceController {
let imageSize38mm = CGSize(width: 100, height: 100)
let imageSize42mm = CGSize(width: 120, height: 120)
}
Run Code Online (Sandbox Code Playgroud)
希望这有助于说明您无法在运行时查询布局和调整大小信息,就像在iOS应用程序中一样.