如何知道AppDelegate中的方向已更改 [英] How can I know the orientation changed in AppDelegate

查看:97
本文介绍了如何知道AppDelegate中的方向已更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The function of how the device know the changement of orientation is -(void)viewWillLayoutSubviews and -(void)viewDidLayoutSubviews But, they are just in the controllers; Now I want to know is there any functions like these to know the changement of orientation in the file 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];
    }

}


推荐答案

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

You can register for notifications when rotation occurs

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

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

Then implement the method that gets called when the message is sent

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

或者查看 UIApplicationDidChangeStatusBarOrientationNotification 将会为您提供

这篇关于如何知道AppDelegate中的方向已更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆