iOS 9: Frame no longer set in viewWillAppear after UINavigationController pushViewController

给你一囗甜甜゛ 提交于 2019-12-29 04:37:26

问题


I'm trying to solve a view placement bug that has arisen as of iOS 9. I am instantiating a view controller from a xib file (non-autolayout) and then pushing this onto my UINavigationController.

The problem is that when the view controller's viewWillAppear method is called, its frame has not yet been adjusted to the navigation controller's size and is still what was set in the xib file. It doesn't get set properly now until viewDidAppear.

This is completely screwing up my code. Does anyone know precisely what has changed that is causing this and what is the best way to handle it? I don't want to wait until viewDidAppear because this will look bad and make for a poor user experience.


回答1:


I am also looking for the best fix.

My temporary one is to call the code that was in "viewDidAppear" in "viewDidLayoutSubviews". That way, my code will get called as soon as the frames are set.

But, make sure to add a boolean or something so that your code doesn't get called every time viewDidLayoutSubviews is called

-(void)viewDidLayoutSubviews{
   if (didLayoutSubviews == NO){
       didLayoutSubviews = YES;
       // perform code that was in viewWillAppear
   }
}



回答2:


I occurred this issue too,

try to uncheck option "Resize View From NIB" from storyboard




回答3:


Try moving the layout code from viewWillAppear to viewWillLayoutSubviews.




回答4:


A little bit updated NickProvost's answer:

private var loadViewToken: dispatch_once_t = 0

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    dispatch_once(&loadViewToken) { [weak self] in
        if let wSelf = self {
            wSelf.setupView()
        }
    }
}


来源:https://stackoverflow.com/questions/32662851/ios-9-frame-no-longer-set-in-viewwillappear-after-uinavigationcontroller-pushvi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!