0

I'm currently working on an Apple TV app. My view is build up from left to right like this (also see screenshot)

  • small UICollectionView
  • UIImageview
  • UIView
    • UILabels
    • UIButton

enter image description here

I can only focus the UIButton on the right when I am on the second uicollectionviewCell.

When I am on the first and third cell I'm not able to get the focus to the UIButton.

Any help on this?

EDIT

I have this code in my ViewDidLoad

view.addLayoutGuide(focusGuide)

    focusGuide.leftAnchor.constraintEqualToAnchor(collectionView.leftAnchor).active = true
    focusGuide.topAnchor.constraintEqualToAnchor(collectionView.topAnchor).active = true
    focusGuide.widthAnchor.constraintEqualToAnchor(view.widthAnchor).active = true
    focusGuide.heightAnchor.constraintEqualToAnchor(view.heightAnchor).active = true

And added this method:

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

        guard let nextFocusedView = context.nextFocusedView else { return }

        switch nextFocusedView {
        case self.collectionView:
            self.focusGuide.preferredFocusedView = self.orderButton

        case self.orderButton:
            self.focusGuide.preferredFocusedView = self.collectionView

        default:
            self.focusGuide.preferredFocusedView = nil
        }
    }
Steaphann
  • 2,797
  • 6
  • 50
  • 109
  • 1
    You're going to have to supply more information about your code. You're asking us to diagnose some bug in your code just by guessing what might be in it. – Stonz2 Jan 14 '16 at 15:04
  • Hi, which part of the code do you need? – Steaphann Jan 14 '16 at 15:07
  • Hi, did you follow all the infos and instructions on this [documentation site](https://developer.apple.com/library/tvos/documentation/General/Conceptual/AppleTV_PG/WorkingwiththeAppleTVRemote.html#//apple_ref/doc/uid/TP40015241-CH5-SW2)? (e.g. is the button disabled? are all the methods implemented?) – anneblue Jan 14 '16 at 15:42
  • [`UIFocusGuide`](https://developer.apple.com/library/tvos/documentation/UIKit/Reference/UIFocusGuide_Class/) –  Jan 14 '16 at 19:58
  • @PetahChristian I am struggling with this UIFocusGuide for a while now... – Steaphann Jan 14 '16 at 20:30
  • Please post some code showing what you tried. –  Jan 14 '16 at 20:35
  • @PetahChristian I've added this in my viewdidload – Steaphann Jan 14 '16 at 21:04

3 Answers3

1

You should double check the code you have in the collecionviewcell of your collectionview regarding to UIFocusEngine delegate calls.

1) Consdier each collectionview as a custom view with UIView as the parent class, follow the guide from Apple's Dev Doc as follows Supporting Focus in Custom Views

Like UIViewController, UIView also conforms to UIFocusEnvironment, meaning that everything outlined in Supporting Focus in View Controllers also applies to custom views. However, because views can be focusable, there are some extra considerations when you implement custom view focus behavior:

If your custom view needs to be focusable, override canBecomeFocused to return YES (by default, it returns NO). Your view might always be focusable or only conditionally focusable. For example, UIButton objects are not focusable when disabled. Optionally override preferredFocusedView if focusing this view should redirect focus to another view (for example, a subview). Override didUpdateFocusInContext:withAnimationCoordinator: to respond to focus updates when they occur and update your app’s internal state.

2) In your focus related delegate method for the collectionview, handle the case when focus updated to another cell, where you want to notify the UIFocusEngine to properly update the context in the new cell.

When you work with collection views and table views, you use a delegate object >to define any custom behavior. This pattern is also used when implementing your >focus-based interface. The UITableViewDelegate and UICollectionViewDelegate >protocols declare methods and properties similar to those provided by the >UIFocusEnvironment protocol, but for table view and collection view behavior.

Tips for supporting focus in collection views and table views:

Use the collectionView:canFocusItemAtIndexPath: method with the UICollectionViewDelegate class or the tableView:canFocusRowAtIndexPath: method with the UITableViewDelegate class to specify whether a specific cell should be focusable. This action works similarly to overriding canBecomeFocused of UIView method in a custom view. Use the remembersLastFocusedIndexPath property, defined in both UICollectionView and UITableView, to specify whether focus should return to the last focused index path when focus leaves and then re-enters the collection view or table view.

there are more to read from https://developer.apple.com/library/tvos/documentation/General/Conceptual/AppleTV_PG/WorkingwiththeAppleTVRemote.html#//apple_ref/doc/uid/TP40015241-CH5-SW2

J.GUI
  • 54
  • 4
0

I can only guess since I didn't work yet on AppleTV but as far as I understand what you are doing, you cannot go to focus the button from the first cell of your collection view. My guess is that you didn't explicitly tell the system what should get the focus from specific elements. The system "see" that the button you want to focus is too far from the first cell on vertical axis. Since the second cell is closer, the system allow it to move.

As I said, it is only a guess from what I learned from Unity3D on automatic link between GUI elements and I may be totally wrong but this looks really a lot like what I said to me.

MessuKilkain
  • 76
  • 1
  • 1
  • 11
0

Can you try the following?

    focusGuide.leftAnchor.constraintEqualToAnchor(imageView.leftAnchor).active = true
    focusGuide.rightAnchor.constraintEqualToAnchor(imageView.rightAnchor).active = true
    focusGuide.topAnchor.constraintEqualToAnchor(imageView. topAnchor).active = true
    focusGuide.bottomAnchor.constraintEqualToAnchor(imageView.bottomAnchor).active = true
HMHero
  • 2,333
  • 19
  • 11