【问题标题】:How to get focused view frame size in tvOS?如何在 tvOS 中获得聚焦的视图帧大小?
【发布时间】:2015-12-22 11:33:53
【问题描述】:

我期待在 tvOS 中获得焦点视图的大小。

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

【问题讨论】:

    标签: swift tvos


    【解决方案1】:

    我认为您正在寻找的是UIImageView 提供的focusedFrameGuide

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

    【讨论】:

    • 感谢您的推荐。不幸的是,我无法测试这是否是正确的答案。
    • 当设置adjustsImageWhenAncestorFocused'framefocusedFrameGuide 不同。所以我认为它可能对你有用。
    【解决方案2】:

    使用自动布局需要设置两个约束:

    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)
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多