Search code examples
iosswiftuiimageviewstoryboardtvos

UIImageView not gets Focused on tvOS


I have the following UIViewController which i am doing in story board.

StoryBoad

The problem i am facing here is that when i try to swipe right on tv remote my UIImageView does not get any focus. Infact no focus event happen, it try to put some logs in

 override func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator)

but there is no focus event updated. Earlier i have UIImageView directly added on self.view in storyboard but i read somewhere the UIImageView itself is not focusable so i put my UIImageView inside another UIView, but it still does not gets any focus. I have aligned top edges of UIView which contains UIImageView and UITextView.

Any help will be highly appreciated

On a side note i also tried to do with UIFocusGuide but that didn't work also. I think i don't need to use UIFocusGuide because it a basic right swipe focus thing.


Solution

  • UIImageView is not by default focusable, and will not get focus effect, you can do two things to make it focusable.

    1. Subclass UIImageView and in subclass override canBecameFocused and return YES, and then override didUpdateFoucsInContext and update appearance for focus state, like if focus make it bigger or change color etc.
    2. Second option is add it to a view which is focusable and then set imageView.adjustImageWhenAncestorFocused = YES also set clipToBounds = NO on superview

    So In your case as you are adding imageView to a view then that view should return YES/True from canBecameFocused.