2014-10-09 27 views
0

我需要在通話狀態增加了狀態欄高度40.我用- (void)application:(UIApplication *)application willChangeStatusBarFrame:(CGRect)newStatusBarFrame使用NSNotificationCenter無法在通話狀態中的ios目標C

它的工作狀態時發佈通知,調整結束的觀點改變的UIView的矩形酒吧高度從20增加到40,但NSNotificationCenter似乎並沒有發佈通知,當狀態欄高度從40降低到20 UIView幀未分配給所需的CGRectMake

我正在一箇舊的項目所以沒有故事板。

這裏就是我試圖

在viewDidLoad中 -

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarChangedForBottmView:) name:@"STATUSBARCHANGED" object:self]; 

和方法

- (void) statusBarChangedForBottmView:(NSNotification *)notification 
{ 
    NSDictionary *data = [notification userInfo]; 
    NSString *statusBar = [data valueForKey:@"frame"]; 
    NSLog(@"status bar in edit card = %@",statusBar); 
    if ([statusBar isEqualToString:@"STATUS"]) 
    { 
     if (STATUSBARINCREASED) 
     { 
      [self.bottomView setFrame:CGRectMake(0, self.view.frame.size.height-60, self.view.frame.size.width, 45)]; 
     } 
     else 
     { 
      [self.bottomView setFrame:CGRectMake(0, self.view.frame.size.height-40, self.view.frame.size.width, 45)]; 
     } 
    } 

} 
+0

你爲什麼不乾脆使用蘋果的'UIApplicationWillChangeStatusBarFrameNotification'? – 2014-10-09 15:49:31

+0

@GuillaumeAlgis我試過了,但沒有奏效。所以我嘗試了一些不同的東西,仍然沒有奏效。不知道爲什麼 – 2014-10-09 15:52:32

+0

我在我的選擇器中添加了斷點,但從未被調用。任何想法爲什麼? – 2014-10-09 15:55:52

回答

1

編輯

確保你,你從訂閱通知任何對象。即:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarChangedForBottmView:) name:@"STATUSBARCHANGED" object:nil]; // <-- object:nil 

這不是當前對象(self),這是張貼通知,這是你的AppDelegate。

從DOC:

notificationSender

的對象,其通知觀察者要接收;也就是說,只有該發件人發送的通知纔會發送給觀察者。如果您通過零,通知中心不會使用通知發件人來決定是否將其交付給觀察員。

如果你得到這個工作,我仍然建議使用UIApplicationWillChangeStatusBarFrameNotification,而不是張貼自己的通知從- (void)application:(UIApplication *)application willChangeStatusBarFrame:(CGRect)newStatusBarFrame


我只測試了一個新的項目,使用UIApplicationWillChangeStatusBarFrameNotification的作品就像一個魅力。

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarChanged:) name:UIApplicationWillChangeStatusBarFrameNotification object:nil]; 
} 

- (void)statusBarChanged:(NSNotification *)notification 
{ 
    NSDictionary *userInfo = notification.userInfo; 
    NSValue *statusBarRectValue = userInfo[UIApplicationStatusBarFrameUserInfoKey]; 
    CGRect statusBarRect = [statusBarRectValue CGRectValue]; 
    NSLog(@"%@", NSStringFromCGRect(statusBarRect)); 
} 

輸出:

接聽電話:

2014-10-09 17:53:31.407 test-so[14775:8742949] { 
    UIApplicationStatusBarFrameUserInfoKey = "NSRect: {{0, 0}, {375, 40}}"; 
} 

通話結束:

2014-10-09 17:53:35.881 test-so[14775:8742949] { 
    UIApplicationStatusBarFrameUserInfoKey = "NSRect: {{0, 0}, {375, 20}}"; 
} 
+0

嘿謝謝..這工作。請你指導我從上面的NSRect中提取40和20嗎? – 2014-10-09 16:07:30

+0

@Wadhiv當然。我更新了我的答案,從通知userInfo字典中提取CGRect。 – 2014-10-09 16:15:47

+0

NSValue * statusBarRectValue = userInfo [UIApplicationStatusBarFrameUserInfoKey]; ?? – 2014-10-09 16:17:41