iOS9:自定义UIWindow使状态栏消失

时间:2015-09-01 04:24:19

标签: ios statusbar ios9 uiwindow

当我在iOS9中创建自定义UIWindow时,窗口会在屏幕上显示,但状态栏突然消失。

当窗口隐藏时,状态栏会再次出现。

下面是我使用Xcode7 beta5在iOS9上获得的2个截图。

隐藏自定义窗口时的状态栏: Status bar while the custom window is hidden

自定义窗口可见时的状态栏: Status bar while the custom window is visible (整个屏幕移到顶部。)

这是我使用的代码(在iOS8上运行良好):

#define DEBUG_SHOWENV_HEIGHT 20.0f

@interface AppDelegate ()
@property (nonatomic) UIWindow*     envWindow;
@end

-(UIWindow*)envWindow
{
    if (_envWindow == nil)
    {
        // Create the window
        _envWindow = [[UIWindow alloc] initWithFrame:CGRectMake(0.0f, self.window.frame.size.height, self.window.frame.size.width, DEBUG_SHOWENV_HEIGHT)];
        _envWindow.rootViewController = [[UIViewController alloc] init]; // added since iOS9 to avoid the assertion
        _envWindow.userInteractionEnabled = NO;
        _envWindow.windowLevel = UIWindowLevelStatusBar;
        _envWindow.backgroundColor = [UIColor colorWithRed:0.243 green:0.471 blue:0.992 alpha:0.8];

        // Make a label
        UILabel* labelEnv = [[UILabel alloc] initWithFrame:CGRectMake(8.0f, 0.0f, _envWindow.bounds.size.width - 16.0f, DEBUG_SHOWENV_HEIGHT)];
        labelEnv.font = [UIFont boldSystemFontOfSize:12.0f];
        labelEnv.textColor = [UIColor whiteColor];
        labelEnv.text = @"DEVELOP ENVIRONMENT";
        [_envWindow addSubview:labelEnv];
    }
    return _envWindow;
}

// ==== in applicationDidBecomeActive

// Show the window 2 seconds then hide it.
[self.envWindow.layer removeAllAnimations];
self.envWindow.frame = CGRectMake(0.0f, self.window.frame.size.height, self.window.frame.size.width, DEBUG_SHOWENV_HEIGHT);
self.envWindow.hidden = NO;
[UIView animateWithDuration:0.25f
                      delay:0.0f
                    options:UIViewAnimationOptionCurveEaseOut
                 animations:^{
                     self.envWindow.frame = CGRectMake(0.0f, self.window.frame.size.height - DEBUG_SHOWENV_HEIGHT, self.window.frame.size.width, DEBUG_SHOWENV_HEIGHT);
                 }
                 completion:^(BOOL finished) {

                     if (finished)
                     {
                         [UIView animateWithDuration:0.25f
                                               delay:2.0f
                                             options:UIViewAnimationOptionCurveEaseOut
                                          animations:^{
                                              self.envWindow.frame = CGRectMake(0.0f, self.window.frame.size.height, self.window.frame.size.width, DEBUG_SHOWENV_HEIGHT);
                                          }
                                          completion:^(BOOL finished) {

                                              if (finished)
                                              {
                                                  self.envWindow.hidden = YES;
                                              }
                                          }];
                     }
                 }];

我很感激任何帮助。

2 个答案:

答案 0 :(得分:5)

解决。 我需要在根视图控制器中实现此方法:

- (BOOL)prefersStatusBarHidden
{
    return NO;
}

由于某些原因,此UIWindow中的根视图控制器隐藏了状态栏。 (虽然默认情况下它应该返回NO),通常是

所以不要这样做:

_envWindow.rootViewController = [[UIViewController alloc] init]; // added since iOS9 to avoid the assertion

我创建了自己的视图控制器,并覆盖了prefersStatusBarHidden。

答案 1 :(得分:0)

<android.support.design.widget.CoordinatorLayout android:id="@+id/product_detail_main_content" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:apptools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.design.widget.AppBarLayout android:id="@+id/appbar" android:layout_width="match_parent" android:layout_height="@dimen/product_detail_app_bar_height" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> <android.support.design.widget.CollapsingToolbarLayout android:id="@+id/detail_product_collapsing_toolbar" android:layout_width="match_parent" android:layout_height="match_parent" android:clipToPadding="true" android:foregroundGravity="bottom|right" android:foregroundTintMode="add" app:contentScrim="?attr/colorPrimary" app:expandedTitleMarginStart="@dimen/space_xxlarge" app:layout_scrollFlags="scroll|exitUntilCollapsed"> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/detail_image_head" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="centerCrop" android:fitsSystemWindows="true" app:layout_collapseMode="parallax" app:layout_collapseParallaxMultiplier="0.7"/> <View android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/gradient_shape" /> </FrameLayout> <android.support.v7.widget.Toolbar android:id="@+id/product_toolBar_title" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:fitsSystemWindows="true" app:layout_collapseMode="pin" app:navigationIcon="@drawable/ic_arrow_back_white_24dp" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/> </android.support.design.widget.CollapsingToolbarLayout> </android.support.design.widget.AppBarLayout> <android.support.v4.widget.NestedScrollView android:id="@+id/detail_product_scrollView" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior"> <android.support.v7.widget.RecyclerView android:id="@+id/product_recycler_Thumbnail" android:layout_width="match_parent" android:layout_height="78dp" android:clipToPadding="false" android:scrollbars="vertical" android:elevation="@dimen/space_low"/> ........ </android.support.v4.widget.NestedScrollView> </android.support.design.widget.CoordinatorLayout> 行将您的窗口置于与状态栏相同的级别(z排序)。我认为您想要的是_envWindow.windowLevel = UIWindowLevelStatusBar;,以便状态栏显示在其上。