Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UICollectionViewCell to UIButton Focus in tvOS

Tags:

ios

tvos

apple-tv

I have a UICollectionView which contains 12-13 UICollectionViewCells. I can easily focus on the UICollectionViewCells and everything works. There is a UIButton outside the UICollectionView. If I am on the first cell or the second cell and I swipe up then I can easily focus on the UIButton. When I am on the third cell then I am not able to move the focus to the UIButton. Any ideas what is going on?

enter image description here

like image 675
john doe Avatar asked Oct 20 '15 15:10

john doe


3 Answers

You should use a UIFocusGuide that encompasses the UIButton you want to focus. Make the UIFocusGuide as wide as the collectionView and tall enough to cover the button. Make the preferredFocusView the button.

  UIFocusGuide *topButtonFocusGuide = [[UIFocusGuide alloc] init];
  topButtonFocusGuide.preferredFocusedView = myButton;
  [self.view addLayoutGuide:topButtonFocusGuide];

  [self.view addConstraints:@[
    [topButtonFocusGuide.topAnchor constraintEqualToAnchor:myButton.topAnchor],
    [topButtonFocusGuide.bottomAnchor constraintEqualToAnchor:myCollectionView.topAnchor],
    [topButtonFocusGuide.leadingAnchor constraintEqualToAnchor:myCollectionView.leadingAnchor],
    [topButtonFocusGuide.widthAnchor constraintEqualToAnchor:myCollectionView.widthAnchor],
  ]];
like image 102
Jess Bowers Avatar answered Nov 15 '22 17:11

Jess Bowers


Jess Bowers solution is almost there but Xcode says UIButtons doesn't have topAnchor property and the solution given by Jess doesn't work for me.

For me, the solution was to create a huge view with the same width as the UICollectionView on top of it. See picture.

enter image description here

The code will be this:

 UIFocusGuide *topButtonFocusGuide = [[UIFocusGuide alloc] init];
  topButtonFocusGuide.preferredFocusedView = myButton;
  [self.view addLayoutGuide:topButtonFocusGuide];

  [self.view addConstraints:@[
    [topButtonFocusGuide.topAnchor constraintEqualToAnchor:transparentView.topAnchor],
    [topButtonFocusGuide.bottomAnchor constraintEqualToAnchor:transparentView.bottomAnchor],
    [topButtonFocusGuide.leadingAnchor constraintEqualToAnchor:transparentView.leadingAnchor],
    [topButtonFocusGuide.widthAnchor constraintEqualToAnchor:transparentView.widthAnchor],
  ]];
like image 38
Duck Avatar answered Nov 15 '22 17:11

Duck


Swift 3.0

Jess Bower's answer was huge help to me & saved a lot of time.

Here's what worked with Swift 3 & tvOS 10. One now has to use preferredFocusEnvironments. In ViewDidLoad()

        self.view.addLayoutGuide(focusGuide)
        self.focusGuide.topAnchor.constraint(equalTo: self.buttonAbove.topAnchor).isActive = true
        self.focusGuide.bottomAnchor.constraint(equalTo: self.CollectionView.topAnchor).isActive = true
        self.focusGuide.leadingAnchor.constraint(equalTo: self.CollectionView.leadingAnchor).isActive = true
        self.focusGuide.widthAnchor.constraint(equalTo: self.CollectionView.widthAnchor).isActive = true
        self.focusGuide.preferredFocusEnvironments = [self.buttonAbove]
like image 35
Peter Wiley Avatar answered Nov 15 '22 16:11

Peter Wiley