Search code examples
iosobjective-cios8uideviceorientationios8-extension

Detect rotation in UIInputViewController subclass. Keyboard extension


I have been developing custom keyboard extension and I need to update few constraints programatically after device rotates. I have been trying to detect user interface rotation in my UIInputViewController subclass but without success. These methods are not called when device rotates:

-(void)willTransitionToTraitCollection:(UITraitCollection *)newCollection withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>) coordinator { NSLog(@"Orientation changed"); }

-(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator { NSLog(@"Orientation changed"); }

I've also tried to observe UIDeviceOrientationDidChangeNotification but it doesn't work either.

Does anyone know how to detect rotation in UIInputViewController?


Solution

  • You're correct - those methods are not called in the UIInputViewController subclass for Keyboard Extensions (not sure about other extension types).

    You need to override viewDidLayoutSubviews.

    For example, if you wanted to update the layout of a UICollectionView subview of your keyboard when the device rotates, you'd do something like this:

    - (void)viewDidLayoutSubviews
    {
        [super viewDidLayoutSubviews];
    
        [self.keyboard.collectionView performBatchUpdates:nil completion:nil];
    }
    

    In your case you can check the orientation of the device in viewDidLayoutSubviews, and then apply/modify constraints appropriately.