2018年11月6日 星期二

UIApplicationWillChangeStatusBarOrientationNotification v.s. UIApplicationDidChangeStatusBarOrientationNotification


結論:
UIApplicationWillChangeStatusBarOrientationNotification
會先被呼叫,而且userInfo 是傳送將要轉換的 orientation。

UIApplicationDidChangeStatusBarOrientationNotification
後被呼叫,但userInfo是傳送轉換前的 orientation。

code:

    [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(statusOrientationWillChange:)
                                               name:UIApplicationWillChangeStatusBarOrientationNotification object:nil];
   
    [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(statusOrientationDidChange:)
                                               name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];

- (void)statusOrientationWillChange:(NSNotification *)notification {
    if (notification.userInfo != nil && notification.userInfo[UIApplicationStatusBarOrientationUserInfoKey] != nil) {
        UIInterfaceOrientation orientation = (UIInterfaceOrientation)[(NSNumber *)notification.userInfo[UIApplicationStatusBarOrientationUserInfoKey] integerValue];
        if (UIInterfaceOrientationIsLandscape(orientation)) {
            self.isLandscape = YES;
        } else {
            self.isLandscape = NO;
        }
    }
   
    [self.tableView reloadData];
}

- (void)statusOrientationDidChange:(NSNotification *)notification {
    if (notification.userInfo != nil && notification.userInfo[UIApplicationStatusBarOrientationUserInfoKey] != nil) {
        UIInterfaceOrientation orientation = (UIInterfaceOrientation)[(NSNumber *)notification.userInfo[UIApplicationStatusBarOrientationUserInfoKey] integerValue];
        if (UIInterfaceOrientationIsLandscape(orientation)) {
            self.isLandscape = YES;
        } else {
            self.isLandscape = NO;
        }
    }
   
}

沒有留言:

張貼留言