如何在tvOS中获得专注的视图帧大小?

时间:2015-12-22 11:33:54

标签: swift tvos

我期待在tvOS中获得专注视图的大小。

我需要它的大小才能将标签从视图中移动到适合焦点框架的新位置。

2 个答案:

答案 0 :(得分:1)

我认为您要找的是focusedFrameGuide提供的UIImageView

使用imageview.focusedFrameGuide.layoutFrame,您可以确定如何重新定位标签。

答案 1 :(得分:0)

使用自动版式,您需要设置两个约束:

focusedConstraint = label.topAnchor.constraint(equalTo: imageView.focusedFrameGuide.bottomAnchor)
unfocusedConstraint = label.topAnchor.constraint(equalTo: imageView.bottomAnchor)

并在didUpdateFocus上启用/停用:

    override func didUpdateFocus(in context: UIFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {
        super.didUpdateFocus(in: context, with: coordinator)

        if context.nextFocusedView == self {
            unfocusedConstraint?.isActive = false
            focusedConstraint?.isActive = true
        } else if context.previouslyFocusedView == self {
            focusedConstraint?.isActive = false
            unfocusedConstraint?.isActive = true
        }

        coordinator.addCoordinatedAnimations({
            self.layoutIfNeeded()
        }, completion: nil)
    }