我是这样解决的。我不确定这是最理想的方式,但它确实有效:
class TableViewCell: UITableViewCell {
var voirToutButton: CustomButton? = CustomButton(color: .red, titleString: "");
var shouldRefocusToVoirToutButton:Bool = false
override var preferredFocusEnvironments : [UIFocusEnvironment] {
return [voirToutButton!]
}
override func shouldUpdateFocus(in context: UIFocusUpdateContext) -> Bool {
if (self.shouldRefocusToVoirToutButton){
// Resetting this to false in order to avoid the focus getting stuck on voirToutButton
self.shouldRefocusToVoirToutButton = false
self.setNeedsFocusUpdate()
self.updateFocusIfNeeded()
}
return true
}
}
extension TableViewCell: UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, shouldUpdateFocusIn context: UICollectionViewFocusUpdateContext) -> Bool {
if let previouslyFocusedItemParent = context.previouslyFocusedItem?.parentFocusEnvironment as? UICollectionView, let nextFocusedItemParent = context.nextFocusedItem?.parentFocusEnvironment as? UICollectionView {
let isPreviousFocusInCurrentCollectionView = previouslyFocusedItemParent == self.moviesCollectionView
let isNextFocusInCurrentCollectionView = nextFocusedItemParent == self.moviesCollectionView
let isFocusLeavingCollectionView = !(isPreviousFocusInCurrentCollectionView == isNextFocusInCurrentCollectionView)
let isFocusMovingDown:Bool = !(context.focusHeading == .up)
if (isFocusLeavingCollectionView && isFocusMovingDown) {
self.shouldRefocusToVoirToutButton = true
}
return true
}
// I remove this else it says that I need to return a bool (!(
else {
print("If I remove this else it says that I need to return a bool (!(")
return true
}
}
}