2013-02-12 89 views
3

改變方向的裝置是怎麼知道方向變化的研究功能是 - (無效)viewWillLayoutSubviews和 - (無效)viewDidLayoutSubviews 但是,他們只是在控制器; 現在我想知道的是有這樣的功能,任何知道方向變化的研究在文件AppDelegate.m我怎麼能知道的AppDelegate

- (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]; 
    } 

} 

回答

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的文檔,它將爲您提供在

0

如果你正在談論的界面的方向,你可以觀察UIApplicationDidChangeStatusBarOrientationNotification如果你想要當設備方向改變通知可以觀察UIDeviceOrientationDidChangeNotification