Teodor Ciuraru
Teodor Ciuraru

Reputation: 3477

How to get focused view frame size in tvOS?

How can I get the size of a focused view in tvOS?

I need its size to move a label from within the view to a new position that fits the focused frame.

Upvotes: 3

Views: 926

Answers (2)

Diogo T
Diogo T

Reputation: 2611

Using Auto Layout you need to set two constraints:

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

And activate/deactivate on 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)
    }

Upvotes: 0

Sense
Sense

Reputation: 181

I think what you are looking for is the focusedFrameGuide that UIImageView provides.

Using the imageview.focusedFrameGuide.layoutFrame you can determine how to reposition your label.

Upvotes: 1

Related Questions