iOS方向更改不起作用也未收到UIDeviceOrientationDidChangeNotification

rob*_*ure 4 iphone notifications rotation orientation ios

我对iOS很陌生,我正拼命想要在我的应用程序中进行方向更改.在研究了我的工作后:

在我的应用程序中,我有一个NavigationController管理七个UIViewController子类.

在项目摘要选项卡中,我激活了所有4个设备方向.

每个UIViewcontroller子类都有一个xib文件,所有xib文件都激活了"autoresize子视图".

UIViewController子类都有:

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait || 
            interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
            interfaceOrientation == UIInterfaceOrientationLandscapeRight );
}
Run Code Online (Sandbox Code Playgroud)

他们也都有:

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
Run Code Online (Sandbox Code Playgroud)

和:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
Run Code Online (Sandbox Code Playgroud)

用NSLog(...)语句实现(从不打印,调试器也从不输入这些方法).

我也试图使用:

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

BOOL getOrientationUpdates = [[UIDevice currentDevice] isGeneratingDeviceOrientationNotifications];
NSLog(@"will receive orientation notifications: %@", getOrientationUpdates?@"YES":@"NO");

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(orientationChanged:)
                                             name:UIDeviceOrientationDidChangeNotification
                                           object:nil];
Run Code Online (Sandbox Code Playgroud)

-(void)orientationChanged: (NSNotification*) notification { NSLog(@"orientationChanged"); }

[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications]; [[NSNotificationCenter defaultCenter] removeObserver:self]; 分别.

当我beginGeneratingDeviceOrientationNotifications在AppDelegate的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 方法中执行等操作时,orientationChanged:在启动时被调用一次,但是我再也没有旋转设备,当我在其中一个UIViewController子类中执行它时,它永远不会被调用!

到目前为止,我想要实现的是获取方向通知来旋转UIImageView和UIImage(在不同的方向上没有任何布局更改).

UIDeviceOrientation o = [UIDevice currentDevice].orientation;
Run Code Online (Sandbox Code Playgroud)

总是回来 UIDeviceOrientationPortrait

可能是我错过了文档或stackoverflow中的内容,但我显然无法弄清楚我需要做什么/添加以使其在我的设置中工作.另外我对stackoverflow很新,所以我希望我的帖子不违反任何平台约定.

任何帮助/提示都非常感谢.非常感谢!

编辑:getOrientationUpdates总是YES,这对我来说很奇怪,因为我旋转它时从不调用通知回调选择器!

编辑:在我的AppDelegate的didFinishLaunchingWithOptions我正在做:

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.regScreenController = [[RegistrationScreenController alloc] initWithNibName:@"RegistrationScreenController" bundle:nil];

navCtrl = [[UINavigationController alloc]initWithRootViewController:self.regScreenController];

[navCtrl setNavigationBarHidden:YES];
self.window.rootViewController = self.regScreenController;
[self.window addSubview:navCtrl.view];
[self.window makeKeyAndVisible];
return YES;
Run Code Online (Sandbox Code Playgroud)

gra*_*ver 5

看看你是否self.window.rootViewController在你的AppDelegate's didFinishLaunchingWithOptions:方法中进行设置,因为如果你只在窗口中添加子视图,则不会触发方向更改通知.