gpt4 book ai didi

autolayout - 如何设置对 tvOS 聚焦元素的约束

转载 作者:行者123 更新时间:2023-12-03 23:31:31 25 4
gpt4 key购买 nike

我有一个 UICollectionView在我的 tvOS 应用程序中,它有一个图像和一个标题文本。我已经设置了一个约束来将标题文本固定在图像下方。

enter image description here

当单元格聚焦并且图像增长时,文本不会移动并停留在图像上。有没有办法设置考虑到聚焦图像大小的约束?

Image without focus

Image with focus

最佳答案

您可以使用 UIImageView属性(property)focusedFrameGuide为了那个原因。它返回一个 UILayoutGuide对象,遗憾的是它不能在 Interface Builder 中使用,但您可以在代码中用它创建约束。请注意,此约束仅在 View 聚焦时才有意义,因此您必须设置其 active属性(property)根据self.focused属性(property)。

首先创建 View 初始化的约束:

self.focusedSpacingConstraint = NSLayoutConstraint(item: imageView.focusedFrameGuide, attribute: .BottomMargin, relatedBy: .Equal, toItem: label, attribute: .Top, multiplier: 1, constant: 0)
//add it to the view and set active to false

然后根据焦点激活此约束或默认约束:
override func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {
coordinator.addCoordinatedAnimations({
self.focusedSpacingConstraint.active = self.focused
self.spacingConstraint.active = !self.focused

//set label's transform and animate layout changes
}

您也可以使用 focusedFrameGuide设置标签的高度(作为图像高度的百分比)。

这种方法的优点是您不必更改(硬编码) constant每当图像大小发生变化时。

关于autolayout - 如何设置对 tvOS 聚焦元素的约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35391551/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com