如何在tvOS中获取焦点视图的大小?
我需要它的大小来将标签从视图内部移动到适合聚焦框架的新位置。
UIImageView
提供的 focusedFrameGuide
。使用 imageview.focusedFrameGuide.layoutFrame
,您可以确定如何重新定位标签。使用自动布局,您需要设置两个约束条件:
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)
}
adjustsImageWhenAncestorFocused
时,frame
与focusedFrameGuide
不同。因此我认为这可能适合您的需求。 - Sense