2013-10-02 34 views
5

我有一個側欄菜單的應用程序,有點像Facebook側欄菜單。我正在使用這個名爲SWRevealViewController的課程,它工作得很好。現在自iOS7出來後,我只是無法弄清楚如何調整我的狀態和導航欄,就像在Facebook應用程序。UIStatusBar在Facebook新的iOS7應用程序

正如您所看到的,在Facebook應用中,當用戶滑動屏幕並打開菜單時,狀態欄將通過淡入淡出動畫從導航欄藍色變爲黑色。在我的應用程序中,用戶滑動屏幕以打開菜單,狀態欄上的藍色(由於該視圖的NavigationBar而呈藍色)不會消失並將顏色更改爲黑色。

facebook app navigation bar

my app navigation bar

同樣,我有另外一個問題,我只是不能在狀態欄的文本顏色更改爲白色。我已經嘗試過所有的東西,互聯網上的每一段代碼,閱讀所有蘋果的文檔,但仍然無法設法改變顏色。

我也用這個代碼:

- (UIStatusBarAnimation)preferredStatusBarUpdateAnimation { 
    return UIStatusBarAnimationFade; 
} 

- (UIStatusBarStyle)preferredStatusBarStyle { 
    return UIStatusBarStyleLightContent; 
} 

- (BOOL)prefersStatusBarHidden 
{ 
    return NO; 
} 

在此先感謝。

============================================== ==========================

UPDATE:在@yoeriboven答案解決這個問題的方法是把2個視圖SWRevealViewController的頂部帶有黑色和藍色,並且只是動畫這兩個,這是一個很好的解決方案,它工作得很好。

所以,當創建revealController時,我創建了2個空白的UIViews並添加它們,一個藍色和一個黑色。其中一個,黑色的,我現在保持隱藏。

self.revealController = [[SWRevealViewController alloc] initWithRearViewController:nil frontViewController:self.frontViewController]; 
self.revealController.delegate = self; 

self.appDelegate.window.rootViewController = self.revealController; 

self.statusBarBlack = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.appDelegate.window.frame.size.width, 20)]; 
[self.statusBarBlack setBackgroundColor:[UIColor blackColor]]; 

self.statusBarBlue = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.appDelegate.window.frame.size.width, 20)]; 
[self.statusBarBlue setBackgroundColor:[UIColor blueColor]]; 

[self.appDelegate.window.rootViewController.view addSubview:self.statusBarBlack]; 
[self.appDelegate.window.rootViewController.view addSubview:self.statusBarBlue]; 

現在,如果你正在使用SWRevealViewController你可以使用下面的代碼,如果沒有,只是建立在你自己的,裏面SWRevealViewController.m只是#import "AppDelegate.h",比這一個替換touchesMoved方法:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [super touchesMoved:touches withEvent:event]; 

    if (self.state == UIGestureRecognizerStateFailed) 
     return; 

    // Change color of status bar by the location of your swipe 
    AppDelegate *appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate; 
    UITouch *currentTouch = [touches anyObject]; 
    CGPoint currentTouchLocationOnWindow = [currentTouch locationInView:appDelegate.window]; 
    appDelegate.splashScreen.blueStatusBar.alpha = currentTouchLocationOnWindow.x/appDelegate.window.frame.size.width; 

    if (_dragging) 
     return; 

    const int kDirectionPanThreshold = 5; 

    UITouch *touch = [touches anyObject]; 
    CGPoint nowPoint = [touch locationInView:self.view]; 

    CGFloat moveX = nowPoint.x - _init.x; 
    CGFloat moveY = nowPoint.y - _init.y; 

    if (abs(moveX) > kDirectionPanThreshold) 
    { 
     if (_direction == SWDirectionPanGestureRecognizerHorizontal) 
      _dragging = YES; 
     else 
      self.state = UIGestureRecognizerStateFailed; 
    } 
    else if (abs(moveY) > kDirectionPanThreshold) 
    { 
     if (_direction == SWDirectionPanGestureRecognizerVertical) 
      _dragging = YES ; 
     else 
      self.state = UIGestureRecognizerStateFailed; 
    } 
} 

現在往下走一點點,尋找方法rightRevealToggleAnimated和這一個取代她:

- (void)rightRevealToggleAnimated:(BOOL)animated 
{ 
    FrontViewPosition toogledFrontViewPosition = FrontViewPositionLeft; 
    if (_frontViewPosition >= FrontViewPositionLeft) { 
     toogledFrontViewPosition = FrontViewPositionLeftSide; 
     [UIView animateWithDuration:0.3 animations:^{ 
      // Change color of status bar 
      AppDelegate *appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate; 
      appDelegate.splashScreen.blueStatusBar.alpha = 0.0; 
     }]; 
    } else { 
     [UIView animateWithDuration:0.3 animations:^{ 
      // Change color of status bar 
      AppDelegate *appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate; 
      appDelegate.splashScreen.blueStatusBar.alpha = 1.0; 
     }]; 
    } 

    [self setFrontViewPosition:toogledFrontViewPosition animated:animated]; 
} 

這應該做的伎倆,享受!

+0

更改狀態欄樣式時,不要忘記在呈現的視圖控制器上調用'setNeedsStatusBarAppearanceUpdate'。 –

+0

我在調用viewDidLoad方法時仍然是這樣。 –

+0

我不確定這是否正確。嘗試viewDidAppear。 –

回答

6

在iOS 7中,您的視圖控制器的視圖是全屏。因此,您可以在視圖的頂部22像素(狀態欄高度)上放置兩個視圖。底部的黑色和頂部的顏色與導航欄相同。使用UIScrollView代理方法之一滑動到SWRevealViewController時,可以計算百分比有多遠,並使用導航欄的顏色將該值應用於子視圖的不透明度。

+0

這是一個很好的解決方案,我爲那些也需要這個的人更新了我的答案。謝啦! –

+0

偉大的幫助你。如果你檢查我的答案是正確的,會很酷。祝你的項目好運。 – yoeriboven

3

我有另一種工作方式,像一個魅力了。

首先在SWRevealView類的SWRevealViewController.h中創建一個View @property,調用underStatusBarView以置於狀態欄位置。

然後在- (id)initWithFrame:(CGRect)frame controller:(SWRevealViewController*)controller方法我實例化@property設置其顏色爲黑色,其Alpha 0

- (id)initWithFrame:(CGRect)frame controller:(SWRevealViewController*)controller 
{ 
    self = [super initWithFrame:frame]; 
    if (self) 
    { 
     _c = controller; 
     CGRect bounds = self.bounds; 

     _frontView = [[UIView alloc] initWithFrame:bounds]; 
     _frontView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight; 

     [self addSubview:_frontView]; 

     CALayer *frontViewLayer = _frontView.layer; 
     frontViewLayer.masksToBounds = NO; 
     frontViewLayer.shadowColor = [UIColor blackColor].CGColor; 

     /* Here starts my under status bar implementation */ 
     CGRect statusFrame = [[UIApplication sharedApplication]statusBarFrame]; 
     _underStatusBarView = [[UIView alloc]initWithFrame:statusFrame]; 
     [_underStatusBarView setBackgroundColor:[UIColor blackColor]]; 
     [_underStatusBarView setAlpha:0.0]; 
     [self addSubview:_underStatusBarView]; 
     /* here it ends */ 

     //frontViewLayer.shadowOpacity = 1.0f; 
     frontViewLayer.shadowOpacity = _c.frontViewShadowOpacity; 
     frontViewLayer.shadowOffset = _c.frontViewShadowOffset; 
     frontViewLayer.shadowRadius = _c.frontViewShadowRadius; 
    } 

    return self; 
} 

然後,我添加的代碼到私有方法負責擺出後視圖單行- (void)_layoutRearViewsForLocation:(CGFloat)xLocation根據前視圖位置更改_underStatusBarView alpha。

- (void)_layoutRearViewsForLocation:(CGFloat)xLocation 
{ 
    CGRect bounds = self.bounds; 

    CGFloat rearRevealWidth = _c.rearViewRevealWidth; 
    if (rearRevealWidth < 0) rearRevealWidth = bounds.size.width + _c.rearViewRevealWidth; 

    CGFloat rearXLocation = scaledValue(xLocation, -_c.rearViewRevealDisplacement, 0, 0, rearRevealWidth); 

    CGFloat rearWidth = rearRevealWidth + _c.rearViewRevealOverdraw; 
    _rearView.frame = CGRectMake(rearXLocation, 0.0, rearWidth, bounds.size.height); 

    CGFloat rightRevealWidth = _c.rightViewRevealWidth; 
    if (rightRevealWidth < 0) rightRevealWidth = bounds.size.width + _c.rightViewRevealWidth; 

    CGFloat rightXLocation = scaledValue(xLocation, 0, _c.rightViewRevealDisplacement, -rightRevealWidth, 0); 

    CGFloat rightWidth = rightRevealWidth + _c.rightViewRevealOverdraw; 
    _rightView.frame = CGRectMake(bounds.size.width-rightWidth+rightXLocation, 0.0f, rightWidth, bounds.size.height); 

    /*this line changes the under status bar view alpha*/ 
    _underStatusBarView.alpha = xLocation/rearRevealWidth; 
} 

你將如何使用rightRevealView你應該改變rearRevealWidhtrightRevealWidth只是爲了防止未來的兼容性問題。

+0

這在縱向方向上工作得很好,但是當我在iPad上進行橫向擺放時,黑色狀態欄在顯示後視圖時並不會完全穿過屏幕的頂部。我該如何解決? – user2330922

0

我真的很喜歡你的方法。我已經對你的代碼做了幾個更新,並且想把它提交給這個項目。如果您對我有任何問題,請告訴我。

+0

請使用此類回覆的評論,請勿使用答案 – markcial

+0

我試過了。不幸的是,爲了評論別人的回答,你必須至少有50的聲譽, – dppeak

+0

請使用其他渠道,如果可以的話,最好保留問題的答覆少,不要混淆谷歌或尋找答案的人,謝謝 – markcial