[当展示ViewController时,我希望视图的生命周期按顺序排列:
注:在4到5之间可以进行viewWill / viewDidLayoutSubviews()的更多迭代。换句话说,我希望viewDidLayoutSubview在第一个viewDidAppear之前触发。
为了测试这一点,我将一个childViewController添加到UIViewController子类中,并调用addSubview以添加childViewController的视图。
override func viewDidLoad() {
super.viewDidLoad()
let childVC = ChildViewController()
self.addChildViewController(childVC)
self.view.addSubview(childVC.view)
}
当我打印出视图方法时,这将产生预期的结果:
当我在UIPageViewController子类上使用setViewController时:
override func viewDidLoad() {
super.viewDidLoad()
let childVC = ChildViewController()
self.setViewControllers([childVC], direction: .forward, animated: true, completion: nil)
}
我总是让viewDidAppear在第一个LayoutSubviews方法之前执行:
有人可以解释为什么会发生这种现象吗?
----进一步的观察---
我还为childViewController上的willMove和DidMove方法添加了日志。初始测试的结果:
willMove(toParentViewController:)
UIPageViewController的结果:
因此看起来setViewController调用了moveToParentViewController,这也许是它较早调用viewDidAppear的原因。
/* @class UIPageViewController */
-(void)_child:(void *)arg2 beginAppearanceTransition:(bool)arg3 animated:(bool)arg4 {
r14 = arg4;
r15 = arg3;
r12 = [arg2 retain];
if ([self _forwardAppearanceMethods] == 0x0) {
objc_unsafeClaimAutoreleasedReturnValue([r12 view]);
[r12 beginAppearanceTransition:r15 & 0xff animated:r14 & 0xff];
}
[r12 release];
return;
}
正如我们在上面看到的,对_forwardAppearanceMethods
的调用必须返回0,这会引起问题,并查看其作用:
/* @class UIViewController */
-(bool)_forwardAppearanceMethods {
r14 = self;
rax = objc_opt_class(self, _cmd);
rbx = @selector(shouldAutomaticallyForwardAppearanceMethods);
rdx = rbx;
if ([rax doesOverrideViewControllerMethod:rdx] == 0x0) {
rbx = @selector(automaticallyForwardAppearanceAndRotationMethodsToChildViewControllers);
}
rax = *_objc_msgSend;
rax = (rax)(r14, rbx, rdx, *_objc_msgSend);
return rax;
}
如我们所见,它的调用shouldAutomaticallyForwardAppearanceMethods
已实现为低且为UIPageViewController
,如下所示:
/* @class UIPageViewController */
-(bool)shouldAutomaticallyForwardAppearanceMethods {
return 0x0;
}
注意它也有:
/* @class UIPageViewController */
-(bool)shouldAutomaticallyForwardRotationMethods {
return 0x0;
}
使用Hopper完成反编译