如何知道AppDelegate中的方向已更改

时间:2013-02-12 16:14:36

标签: iphone ios objective-c screen-orientation

设备如何知道方向变化的功能是   - (void)viewWillLayoutSubviews和 - (无效)viewDidLayoutSubviews 但是,他们只是在控制器中; 现在我想知道是否有类似这样的函数来了解AppDelegate.m文件中方向的变化

- (void)navigationController:(UINavigationController *)navigationController
      willShowViewController:(UIViewController *)viewController
                    animated:(BOOL)animated {

    UINavigationBar *morenavbar = navigationController.navigationBar;
    UINavigationItem *morenavitem = morenavbar.topItem;
    //We don't need Edit button in More screen.
    morenavitem.rightBarButtonItem = nil;
    morenavitem.title = nil;
    UIImage *backgroundImage = [UIImage imageNamed:@"nav.png"];

    [morenavbar setBackgroundImage:backgroundImage 
         forBarMetrics:UIBarMetricsDefault];

    UIDeviceOrientation currentDeviceOrientation = 
           [[UIDevice currentDevice] orientation];
    UIInterfaceOrientation currentInterfaceOrientation = 
           [[UIApplication sharedApplication] statusBarOrientation];
    if (UIDeviceOrientationIsLandscape(currentDeviceOrientation)||
        UIDeviceOrientationIsLandscape(currentInterfaceOrientation)){
        UIImage *backgroundImageLandscape = [UIImage imageNamed:@"navbar_landscape.png"];
        [morenavbar setBackgroundImage:backgroundImageLandscape forBarMetrics:UIBarMetricsDefault];
    }

}

2 个答案:

答案 0 :(得分:7)

您可以在轮换发生时注册通知

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(handleDidChangeStatusBarOrientationNotification:) 
                                             name:UIApplicationDidChangeStatusBarOrientationNotification 
                                           object:nil];

然后实现发送消息时调用的方法

- (void)handleDidChangeStatusBarOrientationNotification:(NSNotification *)notification;
{
  // Do something interesting
  NSLog(@"The orientation is %@", [notification.userInfo objectForKey: UIApplicationStatusBarOrientationUserInfoKey]);
}

或者查看UIApplicationDidChangeStatusBarOrientationNotification的文档,它们将为您提供

答案 1 :(得分:0)

如果您正在谈论界面方向,您可以观察UIApplicationDidChangeStatusBarOrientationNotification,如果您希望在设备方向发生变化时收到通知,您可以观察UIDeviceOrientationDidChangeNotification