以编程方式获取 WKInterfaceGroup 的背景颜色 (WatchKit)

Getting the background color of a WKInterfaceGroup programmatically (WatchKit)

如何以编程方式获取 WKInterfaceGroup 对象的背景颜色?

您可以set the value使用

- (void)setBackgroundColor:(nullable UIColor *)color;

但是你也能得到吗?

看起来没有办法获取背景颜色。

我以前从未使用过任何 WatchKit 的东西,但是在查看 WKInterfaceGroup class 并稍微阅读之后,这是我对 WatchKit 似乎如何工作的理解。

看起来 Apple 希望通信(至少在我看过的文件范围内)是一种方式。也就是说,他们不希望您从手表到应用程序级别检索此类内容。他们想让你告诉手表做什么,而手表只接收这些东西(比如颜色、插图、圆角半径等)

这是我得出的结论(不确定我是否正确)。现在,如果您需要从 WKInterfaceGroup 获取背景颜色,那么您可以继续尝试寻找解决方案。我不知道这是否可能。我建议的是尝试从 WKInterfaceGroup 中找到需要此 属性 的替代解决方案。

您可以在接口对象上设置值,但不能获取其属性的当前值。

这个答案可以在 WKInterfaceObject 超类文档中找到:

Communication between an interface object in your extension and the corresponding interface element in your Watch app is one way. You can set the values of an interface object, but you cannot get the current values. If you want to know the current value of an attribute, you must save the value yourself.

下面是一个示例,说明如何维护您自己的颜色组值:

@IBOutlet weak var myGroup: WKInterfaceGroup!
var myGroupBackgroundColor: UIColor?

// Set the group's color, also save the color for future reference
myGroupBackgroundColor = UIColor.blue()
myGroup.setBackgroundColor(myGroupBackgroundColor)

然后您可以使用 myGroupBackgroundColor 属性.

获取组的当前颜色