From 90a7c1d10c45fcdc94c3464db2b5d22367fd4966 Mon Sep 17 00:00:00 2001 From: chenyilong Date: Wed, 30 Dec 2015 13:02:23 +0800 Subject: [PATCH] [demo4]add method to set navigationController status bar --- README.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/README.md b/README.md index 0481db1..7cb4d2b 100644 --- a/README.md +++ b/README.md @@ -1757,6 +1757,29 @@ bottomlayoutguide 替换成 mas_bottomlayoutguide 所以推荐第一种的方法,不推荐第二种。 我在仓库里给出了 navigation 的两种设置方法,见Demo4。 + + + 第三种方法: + + ```Objective-C + - (UIViewController *)childViewControllerForStatusBarStyle; + ``` + + 按照苹果官方的解释: + + > If your container view controller derives its status bar style from one of its child view controllers, implement this method and return that child view controller. If you return nil or do not override this method, the status bar style for self is used. If the return value from this method changes, call the setNeedsStatusBarAppearanceUpdate method. + + 调用 `setNeedsStatusBarAppearanceUpdate` 时,系统默认会去调用application.rootViewController的 `preferredStatusBarStyle` 方法,所以这时候当前自己的 viewController 的 `preferredStatusBarStyle` 方法根本不会被调用。 + + 这个接口很重要,这种情况下 `childViewControllerForStatusBarStyle` 就有用了。一般我们常用 navigationController 作为 rootViewController,利用此接口便可以很方便自订各个 viewController 的 statusBarStyle。 子类化一个 navigationController,并且 override `childViewControllerForStatusBarStyle` + + ```Objective-C + - (UIViewController * _Nullable)childViewControllerForStatusBarStyle { + return self.topViewController; + } + ``` + + 意思就是说不要调用我自己 `application.rootViewController(navigationController)` 的 `preferredStatusBarStyle` 方法,去调用`childViewControllerForStatusBarStyle` 回传的 UIViewController 的 `preferredStatusBarStyle`。这裡回传 self.topViewController 就可以保证当前显示的 viewController 的 `preferredStatusBarStyle` 会被系统调用且正确的显示。 参考链接: [preferredStatusBarStyle isn't called--For anyone using a UINavigationController:](http://stackoverflow.com/a/19513714/3395008)