I am new to tvOS app. I have created a custom button in UICollectionViewCell
within UICollectionView
. When I load my view controller, I am getting focus to the first cell of the UICollectionView, but by pressing the next key on my keyboard, focus isn't moving to next cell.
I have six cells in my UICollectionView which doesn't require scrolling, this means the cells are fitting properly within the width of UICollectionView. With six cells, I can not move the focus to the next cell. But if I add one more cell to UICollectionView, focus is moving to the next cell properly. I am not sure what's going on. Can anyone please help me out in this?
Thanks
I had this as well with nested collection views. For me, the problem was that the inner collection view did not override -(BOOL)canBecomeFocus
and return NO
.
What helped me debugging this issue is to open the view hierarchy, grab the address of the view (by right-clicking on print description of view) that does not get focused and call po [<view address> _whyIsThisViewNotFocusable]
, this will most likely tell you the problem. In my case it read:
Printing description of $3:
<_UIStackedImageContainerView: 0x7f9404604630; frame = (0 0; 308 308); layer = <_UIStackedImageContainerLayer: 0x7f94046047d0>>
(lldb) po [0x7f9404604630 _whyIsThisViewNotFocusable]
ISSUE: This view returns NO from -canBecomeFocused.
ISSUE: One or more ancestors have issues that may be preventing this view from being focusable. Details:
<StationCollectionViewCell 0x7f940434c6c0>:
ISSUE: This view returns YES from -canBecomeFocused, which will prevent its subviews from being focusable.
<StationSectionCollectionViewCell 0x7f940403e8e0>:
ISSUE: This view returns YES from -canBecomeFocused, which will prevent its subviews from being focusable.
In Swift
If you want to focus collection view Cell then You can use collectionview Delegate method, method name is
func collectionView(collectionView: UICollectionView, didUpdateFocusInContext context: UICollectionViewFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator)
{
}
You can use this method like this...
func collectionView(collectionView: UICollectionView, didUpdateFocusInContext context: UICollectionViewFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {
if let previousIndexPath = context.previouslyFocusedIndexPath,
let cell = collectionView.cellForItemAtIndexPath(previousIndexPath) {
cell.contentView.layer.borderWidth = 0.0
cell.contentView.layer.shadowRadius = 0.0
cell.contentView.layer.shadowOpacity = 0
}
if let indexPath = context.nextFocusedIndexPath,
let cell = collectionView.cellForItemAtIndexPath(indexPath) {
cell.contentView.layer.borderWidth = 8.0
cell.contentView.layer.borderColor = UIColor.blackColor().CGColor
cell.contentView.layer.shadowColor = UIColor.blackColor().CGColor
cell.contentView.layer.shadowRadius = 10.0
cell.contentView.layer.shadowOpacity = 0.9
cell.contentView.layer.shadowOffset = CGSize(width: 0, height: 0)
collectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: [.CenteredHorizontally, .CenteredVertically], animated: true)
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With