Могу ли я наблюдать, когда UIViewController изменяет интерфейсОриентации? - программирование
Подтвердить что ты не робот

Могу ли я наблюдать, когда UIViewController изменяет интерфейсОриентации?

Если у меня есть указатель на UIViewController, могу ли я быть уведомлен, когда он меняет интерфейсОриентирование без изменения кода контроллера?

Лучше всего определить изменения ориентации устройства, а затем посмотреть, будет ли /UIViewController вращаться (d)?

4b9b3361

Ответ 1

Вы можете использовать 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");          
}

Ответ 2

Вы можете использовать метод willAnimateRotationToInterfaceOrientation:duration: на вашем UIViewController, а затем переместить любые 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];
}