我可以观察UIViewController更改interfaceOrientation吗?

如果我有一个指向UIViewController的指针,当它改变interfaceOrientation而不修改控制器的代码时,我可以得到通知吗?

我最好的办法是检测设备方向的变化,然后看看UIViewController是否会旋转(d)?

你可以使用NSNotificationCenter:

  [[NSNotificationCenter defaultCenter] addObserver:self // put here the view controller which has to be notified selector:@selector(orientationChanged:) name:@"UIDeviceOrientationDidChangeNotification" object:nil]; - (void)orientationChanged:(NSNotification *)notification{ UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; //do stuff NSLog(@"Orientation changed"); } 

你可以在你的UIViewController上使用willAnimateRotationToInterfaceOrientation:duration:方法,然后重新定位横向或纵向的任何UIViews(或任何其他代码)。 例如

 - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) { // change positions etc of any UIViews for Landscape } else { // change position etc for Portait } // forward the rotation to any child view controllers if required [self.rootViewController willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration]; }