Clean way to force view to load subviews early

前端 未结 6 1089
南方客
南方客 2021-01-07 23:06

Recently I wrote some code where I tried to refer to an outlet on a UIViewController I\'d just instantiated with [storyboard instantiateViewControllerWith

6条回答
  •  盖世英雄少女心
    2021-01-07 23:24

    I agree that forcing a view to load should be avoided but I ran into a case where it seemed the only reasonable solution to a problem (popping a UINavigationController containing a UISearchController that had yet to be invoked causes a nasty console says warning).

    What I did was use new iOS9 API loadViewIfNeeded and for pre-iOS9 used viewController.view.alpha = 1.0. Of course a good comment above this code will prevent you (or someone else) removing this code later thinking it is unneeded.

    The fact that Apple is now providing this API signals it can be needed from time to time.

提交回复
热议问题