Focus guide in container view without focusable views?

I have a (pretty) simple setup as follows; It is a UIViewController (I will call this the Parent View Controller) with 4 container views and the corresponding child viewcontrollers.


-------------------------------

CV1 | CV 2 | CV3

-------------------------------

CONTAINER VIEW 4



CV 2 will get focus automatically by overriding the necessary methods when the parent view controller loads. It works.


CV1 does not contain any focusable views.


When container view 4 contains only a few items (it is a uicollectionview) swiping up on the remote will just show the tabbar (in which the parent view controller is added to.)


I have tried using the approach with subclassing UILabel and override the isfocusable() method to always return true but with no luck. The method is never called and so I guess that the entire CV1 is not part at all in the focus guide chain.


What is the best approach here? Should I implement the CV1 differently. I have seen this in some forum threads but it seems overkill.


Why does not overriding the isfocusable()-method in a subclass of uilabel to always return true work?


Can i add a focusguide to the entire CV1 somehow? And should I do that? In that case, I could just get the parent view controller, find the correct child viewcontroller and return this view controllers preferred view. But will it work? And I really dont want to do it. 🙂


kind regards from a snowy Sweden

Anders Ericsson

Answered by Frameworks Engineer in 134534022

Can you provide more details about the behavior that you want? Since you say that CV1 has no focusable views, does that mean when swiping up from CV4 you always want focus to go to the contents of CV2?


Generally speaking, UIFocusGuide is the tool to use in situations like this. The parent view controller can configure UIFocusGuides to redirect focus to wherever your app logic determines it needs to go.

Accepted Answer

Can you provide more details about the behavior that you want? Since you say that CV1 has no focusable views, does that mean when swiping up from CV4 you always want focus to go to the contents of CV2?


Generally speaking, UIFocusGuide is the tool to use in situations like this. The parent view controller can configure UIFocusGuides to redirect focus to wherever your app logic determines it needs to go.

Yes, you are correct in your assumption. When focus are in CV4 and below CV1 and swiping up, the CV2 preferred view should be focused.


But is it "OK" to have an entire container view be occupied by a single UIFocusGuide? I would have assumed that subclassing UILabel (as in my case) and overriding the isfocusable-method would do the trick. But it does not work. However, I will implement a uifocusguide and see if it plays out well.

It is fine to have a focus guide that covers over large areas. The focus guide will automatically disable itself if it overlaps with the focused view. So a focus guide that covers CV1, CV2, and CV3 will only affect focus into that combined area, but won't affect focus movement within CV2.


I'm not sure what you are referring to as the "isFocusable" method: do you mean "isFocused" or "canBecomeFocused"? I'm also not sure what you are expecting that achieve: where is this label? Why are you trying to make it focusable? How is that label related to your container views?

Ok. So I did some coding. The problem is solved. But I did a mistake and I am posting this for others.


So, to recap. This is the setup for the Parent View Controller. There are four container views with the corresponding child view controllers.


-------------------------------

CV1 | CV 2 | CV3

-------------------------------

CONTAINER VIEW 4



My problem was that CV1 did not have any focusable views. Therefore, when the CV4 only contained only a few items (CV4 is a UIViewCollection) when swiping up the tabbar would become focused. Which is wrong. The suggestion from Matt was to add a UIFocusGuide to correct this.


So, and here is my mistake, I added a UIFocusGuide to the child view controller for CV1. This did not actually work. So I added a lot of print("") debug messages just so that I could understand what the heck was going on. It turns out, the Parent View Controller will receive all focusable views didUpdateFocusInContext-call from _all_ child view controllers. So, I did this in the Parent View Controller


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

super.didUpdateFocusInContext(context, withAnimationCoordinator: coordinator)

guard let nextFocusedView = context.nextFocusedView else { return }

guard let prevFocusedView = context.previouslyFocusedView else { return }


if context.nextFocusedView is UICollectionViewCell {

focusGuide?.preferredFocusedView = self.childViewControllers[1].preferredFocusedView

}

else{

focusGuide?.preferredFocusedView = nil

}

}


I've added the magical part in bold. The instance variable focusGuide has been added as


focusGuide = UIFocusGuide()

view.addLayoutGuide(focusGuide!)

focusGuide?.leftAnchor.constraintEqualToAnchor(self.CV1?.leftAnchor).active = true

focusGuide?.topAnchor.constraintEqualToAnchor(self.CV1?.topAnchor).active = true

focusGuide?.widthAnchor.constraintEqualToAnchor(self.CV1?.widthAnchor).active = true

focusGuide?.heightAnchor.constraintEqualToAnchor(self.CV1?.heightAnchor).active = true


matching the bounds of CV1.


So, since the CV4 IS a UICollectionView I just check for the type of the nextFocusedView in the if-clause and sets the preferred view of the focusguide to be the preferredFocusView of another childViewController!


Thank you Matt.