2016-04-18 12 views
0

現在のViewControllerのステータスバーにUIViewを表示するコードがあります。このコードは最初のViewControllerで完璧に動作しますが、このコードが別のviewControllerから実行されるとステータスバーは消えますが、ラベルは決してアニメーションも表示もされません。UIViewControllerにUIViewが表示されない

2番目のVCはNavigationControllerに組み込まれています。それが唯一の違いです。

なぜこれが機能していないのか分かりません。

+ (UIView *) initializeSuccessfulSparkViewOnView: (UIView *) view 
{ 
// Get View Width 
CGRect viewRect = view.window.frame; 
CGFloat viewWidth = viewRect.size.width; 

// Custom popup for new users 
UIView *successfulSparkView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, viewWidth, 20)]; 
[successfulSparkView setCenter: CGPointMake(viewWidth/2, -10)]; 
[successfulSparkView setBackgroundColor: [UIColor colorWithHexString: @"FF4300"]]; 

// Border 
[[successfulSparkView layer] setBorderWidth: .30f]; 
[[successfulSparkView layer] setBorderColor: [UIColor whiteColor].CGColor]; 

// Drop shadow 
[[successfulSparkView layer] setShadowRadius: 1.0]; 
[[successfulSparkView layer] setShadowOpacity: 1.0]; 
[[successfulSparkView layer] setShadowOffset: CGSizeMake(.1, .1)]; 
[[successfulSparkView layer] setShadowColor: [UIColor whiteColor].CGColor]; 
[view addSubview: successfulSparkView]; 

// Label For Successful Spark Sent 
UILabel *sparkSentLabel = [[UILabel alloc] initWithFrame: CGRectMake(0, 0, viewWidth, 20)]; 
[sparkSentLabel setCenter: CGPointMake([[successfulSparkView window] frame].size.width/2, 10)]; 

// If Device Model is 4 or 5, Make Smaller Text To Fit Screen 
if ([[UIDevice model] containsString: @"iPhone 4"] || [[UIDevice model] containsString: @"iPhone 5"]) 
{ 
    [sparkSentLabel setFont: [UIFont fontWithName: @"Helvetica Neue" size: 9]]; 
} 
else 
{ 
    [sparkSentLabel setFont: [UIFont fontWithName: @"Helvetica Neue" size: 10]]; 
} 

// Setup Label On UIView 
[sparkSentLabel setTextAlignment: NSTextAlignmentCenter]; 
[sparkSentLabel setTextColor: [UIColor whiteColor]]; 
[sparkSentLabel setText: @"Your Spark was sent successfully, and will be delivered at the selected time."]; 
[successfulSparkView addSubview: sparkSentLabel]; 
[successfulSparkView bringSubviewToFront: sparkSentLabel]; 

return successfulSparkView; 
} 

+ (void) presentSuccessfulSparkSentView 
{ 
//GET CURRENT VIEWCONTROLLER 
UIViewController *currentViewController = [UIViewController currentViewController]; 
UIView *successfulSparkView = [UIViewController initializeSuccessfulSparkViewOnView: [currentViewController view]]; 

CGRect viewRect = successfulSparkView.window.frame; 
CGFloat viewWidth = viewRect.size.width; 

//SHOW SUCCESSFUL VIEWCONTROLLER 
float displayTime = .50; 
[UIView animateWithDuration: displayTime delay: 0 options: UIViewAnimationOptionCurveEaseOut animations:^{ 
    [successfulSparkView setCenter: CGPointMake(viewWidth/2, 10)]; 
    [[[currentViewController view] window] setWindowLevel: UIWindowLevelStatusBar]; 
} 
       completion:^(BOOL finished) 
{ 
}]; 

//DISMISS SUCCESSFUL VIEWCONTROLLER 
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)((displayTime * 4) * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 
    [UIView animateWithDuration: displayTime delay: 0 options: UIViewAnimationOptionCurveEaseOut animations: 
    ^{ 
     [successfulSparkView setCenter: CGPointMake(viewWidth/2, -10)]; 
    } 
        completion:^(BOOL finished) 
    { 
     [[[currentViewController view] window] setWindowLevel: UIWindowLevelNormal]; 
    }]; 
}); 
} 

答えて

1

変更は、この

+ (void) presentSuccessfulSparkSentView 
{ 
//GET CURRENT VIEWCONTROLLER 
    UIViewController *currentViewController = [UIViewController currentViewController]; 
    if(currentViewController.navigationController != nil) 
    { 
     currentViewController = currentViewController.navigationController; 
    } 

...... 

のようなコードでは、私は[のUIViewController currentViewController]が唯一のUIViewControllerを取得していることを考えると、あなたはそれのビュー上のUIViewを追加しよう。

しかし、UINavigationBarはUIViewControllerの上にあるので、UIViewControllerのビューにUIViewを追加すると、UINavigationBarはそれを隠します。

「UIWindow」にUIViewを追加するのが最善の方法です。

あなたは[UIViewController currentViewController]を呼び出す必要はなく、UINavigationViewControllerなどについて気にする必要はありません。

+0

これは機能しました!ありがとうございました!! –

0

[UIViewController currentViewController]で実装を確認してください。フェッチしているコントローラがビューコントローラかナビゲーションコントローラかをチェックする必要があります。後者の場合は、ナビゲーションのルートビューを取得してcurrentViewControllerとして返す必要があります。

関連する問題