简体   繁体   中英

How to restore focus when presented view controller is dismissed?

I am building an app like Photos where you can scroll through thumbnails of your photos in a UICollectionView and you can tap on one to view that photo full-screen. I'm working to add support for keyboard navigation so you can focus on a photo with the arrow keys then press space to view it full-screen, dismiss it, then focus on another photo. This works well in the Photos app, but in my app when you dismiss the full-screen view controller, the focus is not restored - that cell is not focused until you press an arrow key again. How do you restore the focus when the presented view controller is dismissed?

To enable focus I set these in the UICollectionViewController :

collectionView.allowsFocus = true
collectionView.allowsFocusDuringEditing = true
collectionView.remembersLastFocusedIndexPath = true

I thought setting this would do the trick but I get the same behavior:

restoresFocusAfterTransition = true

After a lot of debugging, I determined this was not working because I overrode canBecomeFirstResponder returning true . Once I returned false in both the presenting and presented view controller, focus properly restored to the presenting upon dismissing the presented. I am not sure if this is intentional behavior so I filed a bug report FB9814702.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM